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