I am working with the above files and have precompiled MyClass header to an object file (MyClass.o). Now if I add this precompiled header to my project and try compiling the project I get errors
i.e. [Linker error] undefined reference to MyClass::<all member functions>.
I'm using Dev-c++ ver. 4.9.9.2 IDE and compiler.
Any pointers to my problems will be greatly appreciated, as I'm not sure if I understand precompiled headers.
If I include MyClass.cpp source it compiles ok. Now how can I distribute my header file without including its source code?
#ifndef _MYCLASS_H
#define _MYCLASS_H
#include <string>
using std::string;
class MyClass{
public:
MyClass(const string& text);
const string& text()const;
void setText(const string& text);
int getLengthOfText()const;
private:
string m_text;
};
#endif
#include "MyClass.h"
MyClass::MyClass(const string& text)
: m_text(text){ }
const string& MyClass::text()const{
return m_text;
}
void MyClass::setText(const string& text){
m_text = text;
}
int MyClass::getLengthOfText()const {
return m_text.length();
}
#include "MyClass.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
string myName = "Bill Gates";
MyClass instance(myName);
cout<<"instance text is: "<<instance.text()<<endl;
cout<<"Length of instance text is: "
<<instance.getLengthOfText()<<endl;
string anotherStr = "Yet Another string";
instance.setText(anotherStr);
cout<<"Now instance text is: "
<<instance.text()<<endl;
cout<<"Now length is: "<<instance.getLengthOfText()<<endl;
cout<<"press NEWLINE to continue";
cin.get();
return 0;
}