|
-
Jul 2nd, 2002, 12:08 PM
#1
External cpp
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
-
Jul 2nd, 2002, 12:09 PM
#2
Frenzied Member
you have to include it in the main cpp file
#include "myfile.cpp"
-
Jul 2nd, 2002, 12:25 PM
#3
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 ?
-
Jul 2nd, 2002, 04:54 PM
#4
Monday Morning Lunatic
Put the class definition into a header, and the source into a .cpp file:
myclass.h:
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
myclass.cpp:
Code:
#include <iostream>
#include "myclass.h"
using namespace std;
void myclass::Print() {
cout << m_name << " : " << m_number << endl;
}
main.cpp:
Code:
#include <iostream>
#include "myclass.h"
int main() {
MyClass m("Hello", 25);
m.Print();
m.Name("Mikey");
m.Print();
}
...and so on 
PS: There might be a few typos in there, I did it straight into the reply window which isn't exactly great for coding
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 3rd, 2002, 04:19 PM
#5
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.
-
Jul 3rd, 2002, 05:51 PM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 4th, 2002, 10:22 AM
#7
If I do not have to include CPP taht mean that I need to put all my class in an Header?
-
Jul 4th, 2002, 11:02 AM
#8
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 4th, 2002, 04:54 PM
#9
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
-
Jul 4th, 2002, 04:58 PM
#10
Monday Morning Lunatic
It's a damn good book, that 
It's been my reference on many occasions.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|