这个 c++ 程序没有执行什么?

What is this c++ program not executing?

本文关键字:执行 什么 c++ 程序 这个      更新时间:2023-10-16

我正在测试将类放在单独的文件中的概念,同时执行时出现错误。请帮忙

主文件.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++及其编译的更多信息。阅读有关链接器的详细信息。

请注意,C++源文件是一个翻译单元,通常包含一些头文件。阅读有关预处理器的更多信息。

您最好使用std::string而不仅仅是头文件中的string(因为在头文件中使用using std;是不受欢迎的(。

不要忘记在编译时启用所有警告和调试信息。使用 GCC 编译,使用g++ -Wall -Wextra -g编译。

在实践中,在构建具有多个翻译单元的项目时,您最好使用一些构建自动化工具,例如 GNU make。

请记住,IDE 只是美化的源代码编辑器,能够运行外部工具,如构建自动化工具、编译器、调试器、版本控制系统等......您最好能够在命令行上使用这些工具。