Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++不能从其他文件中使用类 我是C++编写小程序的。我太喜欢处理多个文件了。我一直在使用另一个文件中的类。我做了一个简单的测试项目来演示我的问题。我有3个文件_C++_File_Class_Header - Fatal编程技术网

C++不能从其他文件中使用类 我是C++编写小程序的。我太喜欢处理多个文件了。我一直在使用另一个文件中的类。我做了一个简单的测试项目来演示我的问题。我有3个文件

C++不能从其他文件中使用类 我是C++编写小程序的。我太喜欢处理多个文件了。我一直在使用另一个文件中的类。我做了一个简单的测试项目来演示我的问题。我有3个文件,c++,file,class,header,C++,File,Class,Header,testheader.h #ifndef __testheader_H_INCLUDED__ // if Node.h hasn't been included yet... #define __testheader_H_INCLUDED__ // #define this so the compiler knows it has been included #include <string> #include <iostream> class testhe

testheader.h

#ifndef __testheader_H_INCLUDED__   // if Node.h hasn't been included yet...
#define __testheader_H_INCLUDED__   //   #define this so the compiler knows it has been included

#include <string>
#include <iostream>
class testheader { 
    public:
    testheader(std::string name){}
    void write(){}
};
#endif
testheader(std::string name){}
当我运行另一个可执行文件时,输出是

测试

我所期待的是出局

测试 矿 东西

我的类对象测试似乎编译为null。我不确定是我的头还是文件链接不正确。当在main中创建testheader对象时,它显然没有像预期的那样调用testheader.cpp中的构造函数。你能帮个忙吗

谢谢, Noob

主要事件 在testheader.h中

#ifndef __testheader_H_INCLUDED__   // if Node.h hasn't been included yet...
#define __testheader_H_INCLUDED__   //   #define this so the compiler knows it has been included

#include <string>
#include <iostream>
class testheader { 
    public:
    testheader(std::string name){}
    void write(){}
};
#endif
testheader(std::string name){}
声明并实现一个函数,该函数不做任何事情,而只是定义它并在其他地方实现它。这就是所谓的而不是印刷。你想要

testheader(std::string name);
现在main可以看到函数存在,链接器将查找它,一旦修复2和修复3发生,在testheader.cpp中找到它

下一个 不要编译头文件。头文件的副本包含在包含它的所有文件中。只编译实现文件,所以

g++ -std=c++11 testheader.cpp anotherfile.cpp -o another
第三步:盈利! testheader在testheader.h中定义。只有静态成员的函数和存储的实现需要在testheader.cpp中

示例testheader.cpp:

#include <string>
#include <iostream>
#include "testheader.h" // so it knows what testheader looks like

using namespace std;

testheader::testheader(string name)
{
    cout << name << endl;
}
void testheader::write()
{
    cout << "stuff" << endl;
}
旁注:_testheader_H_INCLUDED__是非法标识符。在关于如何/在何处使用下划线的其他规则中,不要在代码中的任何位置将两个下划线放在一行中

主要事件 在testheader.h中

#ifndef __testheader_H_INCLUDED__   // if Node.h hasn't been included yet...
#define __testheader_H_INCLUDED__   //   #define this so the compiler knows it has been included

#include <string>
#include <iostream>
class testheader { 
    public:
    testheader(std::string name){}
    void write(){}
};
#endif
testheader(std::string name){}
声明并实现一个函数,该函数不做任何事情,而只是定义它并在其他地方实现它。这就是所谓的而不是印刷。你想要

testheader(std::string name);
现在main可以看到函数存在,链接器将查找它,一旦修复2和修复3发生,在testheader.cpp中找到它

下一个 不要编译头文件。头文件的副本包含在包含它的所有文件中。只编译实现文件,所以

g++ -std=c++11 testheader.cpp anotherfile.cpp -o another
第三步:盈利! testheader在testheader.h中定义。只有静态成员的函数和存储的实现需要在testheader.cpp中

示例testheader.cpp:

#include <string>
#include <iostream>
#include "testheader.h" // so it knows what testheader looks like

using namespace std;

testheader::testheader(string name)
{
    cout << name << endl;
}
void testheader::write()
{
    cout << "stuff" << endl;
}

旁注:_testheader_H_INCLUDED__是非法标识符。在关于如何/在何处使用下划线的其他规则中,不要在代码中的任何位置将两个下划线放在一行中

testheader.cpp应包括testheader.htestheader.cpp不应重新定义类。此外,您不能在标头中实现成员函数,也不能在其他任何地方重新实现相同的成员。谢谢大家,我现在已经解决了这个问题。我没有意识到我在头声明的末尾有括号,我不能使用该类两次。不要使用以下划线开头的标识符。这些是为编译器、标准库或system.testheader.cpp中的内容保留的。testheader.cpp应包括testheader.htestheader.cpp不应重新定义类。此外,您不能在标头中实现成员函数,也不能在其他任何地方重新实现相同的成员。谢谢各位,我现在已经解决了。我没有意识到我在头声明的末尾有括号,我不能使用该类两次。不要使用以下划线开头的标识符。这些是为编译器、标准库或系统中的内容保留的。