问题
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
#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
#include <string>
#include <iostream>
using namespace std;
class testheader {
public:
testheader(string name){
cout << name << endl;
}
void write(){
cout << "stuff" << endl;
}
};
anotherfile.cpp
#include <iostream>
#include "testheader.h"
using namespace std;
int main () {
cout << "testing" << endl;
testheader test("mine");
test.write();
return 0;
}
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
testing
what I'm expecting is the out put
testing mine stuff
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?
Thanks, Noob
回答1:
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);
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.
Next
g++ -std=c++11 testheader.cpp anotherfile.cpp testheader.h -o another
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
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.
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;
}
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.
来源:https://stackoverflow.com/questions/50710256/c-cant-use-class-from-another-file