C ++不能使用其他文件中的类 [英] C++ can't use class from another file
问题描述
通过编写小型程序,我是C ++.我也很需要处理多个文件.我被困在使用另一个文件中的类.我做了一个简单的测试项目来演示我的问题.我有3个文件.
I'm C++ by writing small programs. I'm too the point of working with multiple files. I'm stuck on using a class from another file. I made a simple test project to demonstrate my problem. I have 3 files.
testheader.h
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.cpp
testheader.cpp
#include <string>
#include <iostream>
using namespace std;
class testheader {
public:
testheader(string name){
cout << name << endl;
}
void write(){
cout << "stuff" << endl;
}
};
anotherfile.cpp
anotherfile.cpp
#include <iostream>
#include "testheader.h"
using namespace std;
int main () {
cout << "testing" << endl;
testheader test("mine");
test.write();
return 0;
}
我在Linux上使用g ++通过命令将它们全部编译
I compile them all in Linux using g++ with the command
g++ -std=c++11 testheader.cpp anotherfile.cpp testheader.h -o another
当我运行另一个"可执行文件时,输出为
When I run the "another" executable the output is
测试
我期望的是输出结果
测试矿东西
似乎我的类对象"test"正在编译为null.我不确定这是我的标题还是文件未正确链接.当在main中创建testheader对象时,显然不是按预期的那样在testheader.cpp中调用构造函数.你能帮一个菜鸟吗?
It seems my class object "test" is compiling as null. I'm not sure if it's my header or the files aren't linked properly. When the testheader object is created in main it's clearly not calling the constructor in the testheader.cpp as expected. Can you help a noob out?
谢谢,菜鸟
推荐答案
主事件
在testheader.h
The Main Event
In testheader.h
testheader(std::string name){}
声明并实现一个仅执行定义而不在其他地方实现的功能.这就是所谓的而不是打印.您想要
declares and implements a function that does nothing rather than defining it and implementing it elsewhere. This is what is being called and not printing. You want
testheader(std::string name);
现在 main
可以看到该函数存在,并且链接程序将查找该函数(一旦修复了第二个和第三个问题,请在testheader.cpp中找到它.
Now main
can see the function exists and the linker will look for it (and once fix two and three take place, find it in testheader.cpp.
g++ -std=c++11 testheader.cpp anotherfile.cpp testheader.h -o another
不编译头文件.标头文件的副本包含在所有 #include
文件中.只编译实现文件,所以
do not compile header files. A copy of the header file is included in all of the files that #include
it. Compile only the implementation files, so
g++ -std=c++11 testheader.cpp anotherfile.cpp -o another
第三步:获利!
testheader
在testheader.h中定义.仅静态成员的功能和存储的实现需要在testheader.cpp中.
Step Three: Profit!
The testheader
is defined in testheader.h. Only the implementations of the functions and storage for static members need to be in testheader.cpp.
示例testheader.cpp:
Example 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 __
是非法标识符.关于下划线的使用方式/位置的其他规则(在C ++标识符中使用下划线的规则是什么?)永远不要在代码中的任何位置连续放置两个下划线.
Side note: __testheader_H_INCLUDED__
is an illegal identifier. Among other rules on how/where to use underscores (What are the rules about using an underscore in a C++ identifier?) never put two underscores in a row anywhere in your code.
这篇关于C ++不能使用其他文件中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!