-1

我正在使用 gvim 为课程做 c++(因为我们被告知我们必须在 linux 中完成,并且课程是使用 c++ 教授的)。我以前上过 java 课,但是这位老师并没有真正告诉我们如何在 c++ 或 linux 中做事,因为他说这是另一个只使用 c++ 的课程。

我的作业遇到的问题是我们必须创建一些类并让它们相互获取信息,但是每当我尝试编译时我都会遇到错误。(我似乎无法弄清楚如何让他们互相交谈并使用彼此的函数/变量。)

前任:

class a {

string user;
public: string user2;
public: vector<string> friendList;

public: void userName()
{
    cout output
    cin >> user;
}
public: void addFriend()
{
    cout output
    cin >> user2;
    friendList.push_back(user2);
}

public: string getName()
{
    return user;
}
};

(已经尝试了第二类2种方法,但都不起作用)way1--->

class b {
string message, username;
a User;
public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = User.getName();
    }
};

或者这样---->

class b: public a {
string message, username;
a User;
public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = User.getName();
    }
};

(或者有这样的功能:)

    public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = user2;
    }
};

无论哪种方式,这些课程似乎都没有互相交谈,我不知道如何让他们学习,因为这些方式不起作用,这就是书中的内容/到目前为止我在互联网上找到的内容。

所以我想我的问题是我怎样才能让 a 与 b 交谈,以便 b 可以使用来自 a 的函数或变量?需要知道这样类可以相互交谈,并且我可以让主函数也调用每个类(每个类都在一个单独的.cpp 文件中)。

编辑:

(类在不同的文件中)

我为错误的终端制作了一个脚本:

Script started on Sun 29 Sep 2013 02:27:42 PM CDT
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ g++ -c  homepg.cpp
homepg.cpp:14:26: error: expected class-name before ‘,’ token
homepg.cpp:15:1: error: expected class-name before ‘{’ token
homepg.cpp:18:24: error: ‘user’ was not declared in this scope
homepg.cpp:18:24: error: ISO C++ forbids initialization of member ‘userName1’ [-fpermissive]
homepg.cpp:18:24: error: making ‘userName1’ static [-fpermissive]
homepg.cpp:18:24: error: invalid in-class initialization of static data member of non-integral type ‘std::string {aka std::basic_string<char>}’
homepg.cpp:19:19: error: ISO C++ forbids initialization of member ‘counter’ [-fpermissive]
homepg.cpp:19:19: error: making ‘counter’ static [-fpermissive]
homepg.cpp:19:19: error: ISO C++ forbids in-class initialization of non-const static member ‘counter’
homepg.cpp:20:30: error: ‘friendList’ was not declared in this scope
homepg.cpp:20:30: error: ISO C++ forbids initialization of member ‘friends’ [-fpermissive]
homepg.cpp:20:30: error: making ‘friends’ static [-fpermissive]
homepg.cpp:20:30: error: invalid in-class initialization of static data member of non-integral type ‘std::vector<std::basic_string<char> >’
homepg.cpp:22:5: error: ‘cout’ does not name a type
homepg.cpp:23:5: error: ‘cout’ does not name a type
homepg.cpp:24:5: error: ‘cout’ does not name a type
homepg.cpp:29:26: error: ISO C++ forbids declaration of ‘displayHome’ with no type [-fpermissive]
homepg.cpp: In member function ‘int homepg::displayHome()’:
homepg.cpp:31:12: error: ‘messageBuff’ was not declared in this scope
homepg.cpp:45:6: error: ‘nextbrac’ was not declared in this scope
homepg.cpp:53:18: error: ‘userName’ was not declared in this scope
homepg.cpp:64:28: error: ‘friends’ was not declared in this scope
homepg.cpp:85:6: error: ‘count’ was not declared in this scope
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ g++ -c    messageBuffer.cpp
 messageBuffer.cpp: In member function ‘void messageBuffer::postMessage()’:
 messageBuffer.cpp:26:13: error: ‘user’ was not declared in this scope
 messageBuffer.cpp: In member function ‘void messageBuffer::tweetMessage()’:
 messageBuffer.cpp:45:17: error: ‘user’ was not declared in this scope
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ exit
 exit

 Script done on Sun 29 Sep 2013 02:29:16 PM CDT

作为测试,我将我的两个类放在一起,一个可以自己编译,一个需要另一个类的函数/变量,然后尝试编译,它们编译得很好

class a {

*functions and variables for a* }

 class b {
 a A;
 *functions for b* }

如果我在同一目录中的单独文件中尝试调用 a,则此示例中的类 b 不起作用,它给了我在我制作的脚本中遇到的错误。

EDIT2:如果我放入一个主函数并让它调用a和b类的函数,我也会在测试文件中得到一个错误。(我知道这个错误是由于没有声明函数,但是如果我尝试在他们的类中声明它们,它们会给我另一个错误是:type::functionname 不能被重载,但是如果我同时取出 a 和 b 的声明编译好并且b可以使用a的函数,那么为什么没有函数声明它们可以正常但主函数不能呢?如果我不能在类中包含函数的声明,我应该把它们放在哪里,因为它说它不能超载它们?)

4

2 回答 2

2

首先 - 你只需要写一次“public:”,然后所有的都是公共的(直到你写“private:”或类似的)。

第二 - 你的第一个“b级”似乎是正确的。

第三 - 两个类都写在同一个文件中吗?还是在不同的文件中?如果在不同的文件中 - 您是否在文件a中包含了文件 for b?它们是.h文件还是.cc文件?

编辑:它应该是这样的,现在让我们说一个文件:

一个文件,main.cc:

#include <string>
#include <vector>
#include <iostream>
using namespace std;

class a {
  string user;
public:
  string user2;
  vector<string> friendList;

  void userName()
  {
      cout <<"blah blah"<<endl;
      cin >> user;
  }
  void addFriend()
  {
      cout <<"blah blah"<<endl;
      cin >> user2;
      friendList.push_back(user2);
  }

  string getName()
  {
      return user;
  }
};

class b {
  string message, username;
  a User;
public: 
  void postMessage()
  {
      cout <<"blah blah"<<endl;
      getline(cin, message);

      username = User.getName();
  }
};

int main(){
  b test;
  test.postMessage();
}

显然,您的类存在许多问题-但这至少应该可以编译。

于 2013-09-29T01:54:52.517 回答
1

这里有一些提示可以让你动起来!

为每个文件定义一个class,并将头文件 ( *.h) 包装在include guards中。

文件a.h

#ifndef A_H
#define A_H

#include <string>
#include <vector>
#include <iostream>

class a // This class needs a better name!
{
private: // Class members are private by default but it's best to be explicit
    std::string user; // When using classes from the C++ Standard Library in a 
                      // header file, specify the `std` namespace explicitly

public: // Everything below here is public
    std::vector<std::string> friendList;

    void userName()
    {
        std::cout << "User name: "; // Note the use of `std::` here otherwise you'll see
                                    // ‘cout’ does not name a type
        std::cin >> user;
    }

    void addFriend()
    {
        std::cout << "Add friend: ";
        std::string user2;
        std::cin >> user2;
        friendList.push_back(user2);
    }

    std::string getName()
    {
        return user;
    }
};

#endif

文件b.h

#ifndef B_H
#define B_H

#include <string>
#include <iostream>
#include "a.h" // Tell the pre-processor to include the entire contents of `a.h`,
               // so that the code below knows what `class a` is.

class b 
{
private:
    std::string message, username;
    a User;

public: 
    void postMessage()
    {
        std::cout << "Post Message: ";
        std::getline(std::cin, message);

        username = User.getName();
    }
};

#endif

文件main.cpp

这是您需要编译的唯一文件,因为它包含b.h(并包含a.h在其中)。

#include "b.h"

int main()
{
    b my_b;
    my_b.postMessage();
}

在上面的代码中,像“Way 1”一样,class b 包含一个class a.

如果你想class b扩展class a比如“Way 2”,你会使用这样的东西:

class b: public a
{

// [...]

public: 
    void postMessage()
    {
        std::cout << "Post Message: ";
        std::getline(std::cin, message);

        username = getName();
    }
};

编译

要么使用终端:

g++ -I/usr/local/include -Wall -Wextra -pedantic main.cpp -o main

或者使用像SingleCompile这样的 Vim 脚本。

于 2013-09-29T02:46:56.117 回答