I have added a class in an other cpp, and I want to use it, how ? Do I need to do something because it seem that it does not see it grrr ;)
Printable View
I have added a class in an other cpp, and I want to use it, how ? Do I need to do something because it seem that it does not see it grrr ;)
you have to include it in the main cpp file
#include "myfile.cpp"
:)
thx but now I have an other problem :error C2086: 'cxHandle' : redefinition
insideLogin.cpp
What is that ? both have #include "mainHeader.h" and in mainHeader.h I have an other header called util.h who have the cxHandle but why the debugger tell me redefinition and how can I fix that ?
Put the class definition into a header, and the source into a .cpp file:
myclass.h:myclass.cpp:Code:#ifndef MYCLASS_H
#define MYCLASS_H
#include <string>
class MyClass {
public:
MyClass(const std::string &name, int number)
: m_name(name), m_number(number) { }
const std::string& Name() const { return m_name; }
void Name(const std::string &nm) { m_name = nm; }
int Number() const { return m_number; }
void Number(int num) { m_number = num; }
void Print();
private:
std::string m_name;
int m_number;
};
#endif // MYCLASS_H
main.cpp:Code:#include <iostream>
#include "myclass.h"
using namespace std;
void myclass::Print() {
cout << m_name << " : " << m_number << endl;
}
...and so on :)Code:#include <iostream>
#include "myclass.h"
int main() {
MyClass m("Hello", 25);
m.Print();
m.Name("Mikey");
m.Print();
}
PS: There might be a few typos in there, I did it straight into the reply window which isn't exactly great for coding ;)
parksie I know that you have already take a look at my little project and I have used #define but it doesn't work, can you take an other look please, juste 2 sec.
The #defines have to be right at the very outer edges of the file. Also, the class definitions all have to be in headers.
PS: Don't include .cpp files.
If I do not have to include CPP taht mean that I need to put all my class in an Header?
All the class definitions (class whatever { };) need to go into a header, and the code for the functions in those classes need to go into a .cpp file.
Well, you can write small functions inline inside the header and they'll get expanded for extra performance (but big functions won't, so it's best to put those in source files).
well I have bought Thinking in C++ Second Edition Vol 1 and I might more understand where to put stuff because in Sam's Teach yourself C++ in 21 days all example are in 1 .cpp.
Thx bro for all your time :)
It's a damn good book, that :)
It's been my reference on many occasions.