Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends...
I am declaring object of a class in one cpp file and calling functions using that object. Now I want to use same object in another cpp file. I am trying to declare that object in another file using extern, but linker gives me an error that object is already defined.

First cpp file:
C++
CClientSock client;
lv_status = client.startup();
if (lv_status != 0)
{
    MessageBox("Connection Failed");
    exit(1);
}


Second cpp file:
C++
extern CClientSock client;
client.connectSocket();


Please suggest the solution. Kindly let me know if more info is required. Thank you!
Posted
Updated 14-May-10 1:06am
v3

Hi .. I changed the name of object 'client' & the problem got resolved . Dont know how ...:)
 
Share this answer
 
Comments
Aescleal 28-May-10 9:18am    
What you've shown there is that you've probably got another object called "client" somewhere in your code or one of the libraries you link with. Quick tip - don't use object at global scope, pass them around as parameters instead, it's a lot less grief.

Cheers,

Ash
Classes cannot be extern. Declare classes in a header file and use them instead. :)

http://publib.boulder.ibm.com/infocenter/macxhelp/topic/com.ibm.vacpp6m.doc/language/ref/clrc03extsc.htm[^]
 
Share this answer
 
v2
Comments
mikhil10 14-May-10 8:01am    
Thanks for yor suggestions ...
Actually I need to use the same instance of class in both cpp files . i.e object calls some functions in 1st cpp thn it calls other functions in another cpp files. Anyways above code worked by changing object name . Linker must be facing some kind of ambiguity .
hakz.code 26-May-11 7:49am    
If object names are different in both the source files then how is the same object being externed and modified?!
If you get object is already defined then it you may have defined it in header file.
Aescleal 28-May-10 9:15am    
Reason for my vote of 1
Classes can't be extern but objects can be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900