我正在测试第一次将类放在单独文件中的概念,但在执行时出现错误。请帮忙
main.cpp这是主文件
#include <iostream>
#include <string>
#include "newClass.h"
using namespace std;
int main()
{
newClass obj1("mayan");
cout << obj1.doneName() << endl ;
}
newClass.h这是单独的头文件
#ifndef NEWCLASS_H
#define NEWCLASS_H
#include <iostream>
#include <string>
#include <string>
class newClass{
private:
string name;
public:
newClass(string z) ;
string doneName();
};
#endif // NEWCLASS_H
这是单独的newClass.cpp文件
#include "newClass.h"
#include <iostream>
#include <string>
using namespace std;
newClass::newClass(string z)
{
name = z ;
}
string newClass :: doneName()
{
return name;
}
最佳答案
您需要阅读更多关于 C++ 及其 compilation 的内容.阅读更多关于 linker 的信息.
注意 C++ 源文件是 translation unit通常 includes一些头文件。阅读更多关于 preprocessor 的信息.
你最好在头文件中使用 std::string
而不仅仅是 string
(因为 using std;
在头文件)。
不要忘记在编译时启用所有警告和调试信息。用GCC , 用 g++ -Wall -Wextra -g
编译。
在实践中,你最好使用一些 build automation工具,例如 GNU make在构建具有多个翻译单元的项目时。
记住 IDE我们只是美化了source code editors能够运行构建自动化工具、编译器、调试器、版本控制系统等外部工具...您最好能够在命令行上使用这些工具。
关于c++ - 这个c++程序没有执行什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136161/