Click here to Skip to main content
14,216,574 members
Rate this:
Please Sign up or sign in to vote.
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:
CClientSock client;
lv_status = client.startup();
if (lv_status != 0)
    MessageBox("Connection Failed");

Second cpp file:
extern CClientSock client;

Please suggest the solution. Kindly let me know if more info is required. Thank you!
Updated 14-May-10 1:06am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi .. I changed the name of object 'client' & the problem got resolved . Dont know how ...:)
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.


Rate this:
Please Sign up or sign in to vote.

Solution 2

Classes cannot be extern. Declare classes in a header file and use them instead. :)[^]
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 .
Aescleal 28-May-10 9:15am
Reason for my vote of 1
Classes can't be extern but objects can be.
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100