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

标签 c++

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

ma​​in.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/

相关文章:

c++ - 未命名的命名空间

c++ - 是否可以在 C++ 中使用 lambda 代替类?

c++ - 在窗口操作系统中从 c 命令启动应用程序

c++ - 为什么这会给出大小为 8 的未初始化值的使用

c++ - UUID 和 uuid_t 数据类型在赋值后可以更改吗?

c++ - ofstream 不会将不同的数据写入两个不同的文件

c++ - 返回 std::function 的类 std::bind 函数

c++ - 通过四舍五入 boost rational_cast ?

C++:创建对象时程序崩溃

c++ - 伪随机数生成器给出相同的第一个输出,但随后按预期运行