Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sharing a class between 2 CPP files 1

Status
Not open for further replies.

ADoozer

Programmer
Dec 15, 2002
3,487
AU
hi, im having a nightmare trying to share a class between 2 files, ive searched this site and google and nothing seems to work.

scenario:
the class is extremely simple (2 member functions, constructor, destructor, 2 variables (both bool))

the class works fine when i declare it within a C++ file and use it there, however i want to also modify the variables (via the functions) from a different C++ file.

i tried declaring the class globaly from the first file (it showed up in my globals folder for the project) but the second file said it was undeclared. (also tried the other way, same problem)

ive tried using extern keyword in stdafx.h (as was suggested on experts-exchange) and just declaring the class once but that errored (LNK2005 [something about the symbol being defined elsewhere]).

i dont want to resort to global variables, but at the moment thats the only way i can get it working.

appreciate any help, my brains fried at the moment.

If somethings hard to do, its not worth doing - Homer Simpson
 
Put the class declaration in a .h file and the implimentations of its methods in a cpp file, and #include the .h file in all the files where the class is used.
 
i have done that.

the class works perfectly well.

my problem is how to declare the 1 class that both CPP files can access.

i was lead to believe that:

extern myClass theClass;

should be placed in somewhere like <stdafx.h> as it is a very common file.

and that i should declare the class:

myClass theClass;

somewhere accessable to both CPP files.

however i keep getting linker errors saying things along the lines of "the identifier is defined in somefile.cpp"

thnx

If somethings hard to do, its not worth doing - Homer Simpson
 
Oh I see. Your problem isn't that both files can't access the same class, it's that both classes cann't access the same object (there's a major difference). There are a few ways you can handle this problem. You could use globals (not recommended, as you note) or preferably you could declare the object in one file and simply make a pointer to it everywhere else you need it. Do that and you shouldn't have any problems.
 
> myClass theClass;
> somewhere accessable to both CPP files.

Globals should be defined only once in one of your .cpp files. To make it accessible, include definition
extern myClass theClass;
to your .h file.
 
hmm.. seems ive made a hash of explaining it... heres a few more details

the code(project) is made up of 4000+ classes and compiles into an LTO file, which is then rez'ed and used by a game.

im not entirely sure of the flow of the program as ive never been able to debug. however i know that one of the classes i want to modify my class with executes in a tight loop.

if i declare my class, in classA i can create a pointer to it in classB (its in the global variable list) but when i compile, the compiler says the global isnt defined (myClass *g_pmyClass=&theClassObject) and visa versa

if i declare my class higher up, i get the following error
Code:
LNK2005 "class myClass g_myClass" (?g_myClass@@3VmyClass@@A) already defined in somefile.obj

hope that makes this a little clearer.

thanks

If somethings hard to do, its not worth doing - Homer Simpson
 
is your .h file wrapped in guards, so it is only included once, no matter how many pages #include the .h file?

have you ttied rebuilding the project from scratch?

K
 
the class i added (through insert class in the menu) has the usual

if !defined statement

however instead of the usual

if !defined(AFX_MYCLASS_H) syntax... it has

if !defined(AFX_MYCLASS_H_BUNCH OF RANDOM LETTERS_INCLUDED_)

could that cause a problem?!?

(this is the first multi project workspace ive worked on, and also the biggest project, so please bear with me on this)

If somethings hard to do, its not worth doing - Homer Simpson
 
no, the random strings just makes the name more unique.

Hmm
have you tried searching on the project name, to make sure that someone else isnt doing something to it?

And have you tried to rebuild from scratch? (its in one of th emenu's. Theres a chance you have incremental build set, and thats causing issues)
 
im pretty sure theres not a variable set already.

i us g_cGameNameClassName for my class object

and searching the project files returns nothing (other than what ive added)

um as for the incremental build (Project->settings->link tab) link incrementally is not set.

im really confused.

(if it would make life easier i could post the code on my site with pointers to the offending class/code [about 12meg zipped])


If somethings hard to do, its not worth doing - Homer Simpson
 
> seems ive made a hash of explaining it

I think one of your problems comes from the fact that you continue to say "class" when you seem to want to be saying "object."

This is a class:
Code:
class Foo {
  public:
    Foo();
  private:
    int bar;
};

This is an object:
Code:
Foo f1;

You keep referring to the latter as a class declaration. You also keep saying nonsensical things like "create a pointer to a class."

I think you need to take a minute to learn the difference between the terms so you can rewrite your orignal question so that the words "class" and "object" are used correctly.


Does this help?

[tt]foo.h[/tt]
Code:
#if !defined FOO_H
#define FOO_H

class Foo {
  public:
    Foo( int b );
    int getBar() const;
  private:
    int bar;
};

extern Foo TheGlobalFoo;

#endif

[tt]foo.cpp[/tt]
Code:
#include "foo.h"

Foo::Foo( int b )
: bar( b ) {
}

int Foo::getBar() const {
    return bar;
}

Foo TheGlobalFoo( 5 );

[tt]main.cpp[/tt]
Code:
#include "foo.h"

int main() {
    int x = TheGlobalFoo.getBar();
}


On second thought...:

> LNK2005 "class myClass g_myClass"
> (?g_myClass@@3VmyClass@@A)
> already defined in somefile.obj

Um... that says something with the name you picked was already defined somewhere. With a project 4,000+ classes big, it's likely that the name you picked was simply already taken.

Try calling it something else; something more unique. See why globals are bad?
 
im self taught, i apologise if my wording is at fault.

however your solution worked.

thanks

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top