我正在使用 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的函数,那么为什么没有函数声明它们可以正常但主函数不能呢?如果我不能在类中包含函数的声明,我应该把它们放在哪里,因为它说它不能超载它们?)