Classes in C++
Hello,
I'm using MS.Net and have declared my own class. I've placed the class declaration in main.h
I want to be able to access objects from many functions. Where would I define each instance of my class?
If I declare it in main(), then I can't reference the class in any other functions. Do I need to be using pointers?
Scooter
You need to pass the objects by parameter, as per
ReturnType FunctionName(ParameterName)
like
void my_function(int x) {}
ReturnType FunctionName(ParameterName)
like
void my_function(int x) {}
I have a simple class called MechWarrior
class Cmechwarrior
{
public:
float xpos;
float ypos;
float zpos;
float heading;
float rotation;
};
The above declaration is in main.h
How do I actually start declaring objects so that I can access them in all my code?
I tried placing the following code in main.cpp
Cmechwarrior mech01
but then I couldn't use mech01 anywhere else...
Scooter
class Cmechwarrior
{
public:
float xpos;
float ypos;
float zpos;
float heading;
float rotation;
};
The above declaration is in main.h
How do I actually start declaring objects so that I can access them in all my code?
I tried placing the following code in main.cpp
Cmechwarrior mech01
but then I couldn't use mech01 anywhere else...
Scooter
To put it easy: Objects enter in action when you instance them and access them like you would do with any variable.
Their scope and life-time is like variables too.
If you declare an instance of an object inside a function they will live until the function ends.
If you declare them globaly (ie: at the beginning of your source file, outside any function) then they'll last until the program ends, and you can access them from any (standalone) function.
Their scope and life-time is like variables too.
If you declare an instance of an object inside a function they will live until the function ends.
If you declare them globaly (ie: at the beginning of your source file, outside any function) then they'll last until the program ends, and you can access them from any (standalone) function.
Thanks all for your VERY FAST help!
Zahlman, I'll check out your link more closely tomorrow as it's getting late here in Canada... but I did have a quick look and yes, I think it's going to answer my question.
Scooter
Zahlman, I'll check out your link more closely tomorrow as it's getting late here in Canada... but I did have a quick look and yes, I think it's going to answer my question.
Scooter
Zahlman,
The link you mentioned was very helpful, however it never stated where to declare class objects.
I now know that I have to place the class declaration in the header, but I'm still not sure how to declare each object.
For example, if I try this:
main.h
Cmechwarrior mech01;
I get this error
KVC error LNK2001: unresolved external symbol "class Cmechwarrior mech01" (?mech01@@3VCmechwarrior@@A)
I also tried:
extern Cmechwarrior mech01;
If I place the declaration in a function, then it seems that none of the other functions can see the object.
Basically, I'd like to create a class called Cmechwarrior, which I can use throughout my program.
Any help with this would be greatly appriciated.
Scooter
The link you mentioned was very helpful, however it never stated where to declare class objects.
I now know that I have to place the class declaration in the header, but I'm still not sure how to declare each object.
For example, if I try this:
main.h
Cmechwarrior mech01;
I get this error
KVC error LNK2001: unresolved external symbol "class Cmechwarrior mech01" (?mech01@@3VCmechwarrior@@A)
I also tried:
extern Cmechwarrior mech01;
If I place the declaration in a function, then it seems that none of the other functions can see the object.
Basically, I'd like to create a class called Cmechwarrior, which I can use throughout my program.
Any help with this would be greatly appriciated.
Scooter
I think CJH was onto what you're looking for. The ideal way to usually deal with this problem is to pass an object as a parameter to a function. Consider this pointless example:
Now you're going to have the problem you described, because Stuff is declared from within main(), and thus is local to main(). PrintStuff doesn't know that Stuff exists. What you want to do is tell PrintStuff what it is you want it to print. You want to inform it of the object Stuff. You usually want to do this by passing Stuff as an argument.
Even better, change those last bits so that it passes a reference to the object (notice the extra ampersands (&)), and doesn't just make a copy of the object:
The alternative to all this is to declare all your objects as globals, which makes accessing them easy, but you'll usually end up with messier, harder to maintain code, so I won't address that at the moment.
//Main.h#include "Elsewhere.h"class CStuff{ public: int mInteger;};//Main.cpp#include "Main.h"int main(){ CStuff Stuff; Stuff.mInteger = 5; PrintStuff(); return 0;}//Elsewhere.h#include "Main.h"#include <iostream>void PrintStuff();//Elsewhere.cpp#include "Elsewhere.h"void PrintStuff(){ std::cout << Stuff.mInteger << std::endl;}
Now you're going to have the problem you described, because Stuff is declared from within main(), and thus is local to main(). PrintStuff doesn't know that Stuff exists. What you want to do is tell PrintStuff what it is you want it to print. You want to inform it of the object Stuff. You usually want to do this by passing Stuff as an argument.
//Main.h#include "Elsewhere.h"class CStuff{ public: int mInteger;};//Main.cpp#include "Main.h"int main(){ CStuff Stuff; Stuff.mInteger = 5; PrintStuff(Stuff); return 0;}//Elsewhere.h#include "Main.h"#include <iostream>void PrintStuff(CStuff SomeStuff);//Elsewhere.cpp#include "Elsewhere.h"void PrintStuff(CStuff SomeStuff){ std::cout << SomeStuff.mInteger << std::endl;}
Even better, change those last bits so that it passes a reference to the object (notice the extra ampersands (&)), and doesn't just make a copy of the object:
//Elsewhere.h#include "Main.h"#include <iostream>void PrintStuff(CStuff& SomeStuff);//Elsewhere.cpp#include "Elsewhere.h"void PrintStuff(CStuff& SomeStuff){ std::cout << SomeStuff.mInteger << std::endl;}
The alternative to all this is to declare all your objects as globals, which makes accessing them easy, but you'll usually end up with messier, harder to maintain code, so I won't address that at the moment.
Define it as extern in a header. Like you said:
extern Cmechwarrior mech01;
That only says that that variable should be globally available. Then you should create it in one sourcefile:
Cmechwarrior mech01
for example:
//main.h
...
extern Cmechwarrior mech01;
...
//main.cpp
...
Cmechwarrior mech01
...
All files will have access to that variable in main.cpp if they include main.h
extern Cmechwarrior mech01;
That only says that that variable should be globally available. Then you should create it in one sourcefile:
Cmechwarrior mech01
for example:
//main.h
...
extern Cmechwarrior mech01;
...
//main.cpp
...
Cmechwarrior mech01
...
All files will have access to that variable in main.cpp if they include main.h
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement