Sign in to follow this  
Scooter911

Classes in C++

Recommended Posts

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

Share this post


Link to post
Share on other sites
You need to pass the objects by parameter, as per

ReturnType FunctionName(ParameterName)

like

void my_function(int x) {}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

//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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this