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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

little confused about extern 2

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
I am new to C++ so please bare with me. I have a member function prototype in the header file which is private. I will call it priv_A. Then I have the implementation of that function in the cpp file. (I understand this is the 'normal' way to do it... Someone please correct me if I'm wrong) I also have another member function which is public that calls this private function. I will call it pub_B.
Okay, so my cpp file compiles/links fine. No problem.
So now I want to call this object from another program which is in another cpp file and use pub_B. However on the link step I get a complaint about the priv_A function which is private. I thought that maybe if I use extern on priv_B, compiler would know that it's definition is in another file, but instead I get errors when compiling.
Can someone please clarify the use of extern and what the correct way of implementing this is? Many thanks to you!


Barry
 
You don't need extern for that.

I don't know where priv_B came from or what it has to do with the other stuff.

Apparently, you're calling priv_A from something that's not a member function and not a friend of the class.

Is it possible that when you define these functions in the .cpp file, you forget to put
Code:
XXX::
where XXX is the name of the class you intend them to be members of?

If not, post some code.
 
Since you haven't posted any code or specific compiler errors I will just give you this

[[pp.h]]
[tt]class PP {
private:
int priv_A();
public:
int pub_B();
};
[/tt]

[[pp.cpp]]
[tt]#include "pp.h"

int PP:priv_A()
{
return rand();
}

int PP::pub_B()
{
return priv_A();
}
[/tt]

[[another.cpp]]
[tt]#include "pp.h"

int JoeFunction()
{
PP thePeePee;
return thePeePee.pub_B();
}
[/tt]

Without any code examples, that's all I can give, but I hope that clarifies things. Notice how we didn't need to use extern anywhere. If you want an example of the proper use of extern, just tell me and I'll post it on here for you.

I REALLY hope that helps.
Will
 
Ah Ha! But I am getting the same link problem even with your example, Will. Here is the error I get when compiling another.cpp
Code:
/out:another.exe
/debug
another.obj
another.obj : error LNK2019: unresolved external symbol "public: int __thiscall PP::pub_B(void)" (?pub_B@PP@@QAEHXZ) referenced in function
"int __cdecl JoeFunction(void)" (?JoeFunction@@YAHXZ)
another.exe : fatal error LNK1120: 1 unresolved externals

So it must be something I am doing wrong. I compile via command line like this:
cl /GZ /GX /Zi another.cpp

Any ideas?

And yes, an example of where you would use extern would also be useful.
Thanks!

chipperMDW, priv_B was just a typo. Sorry, and thanks for your input too.
 
Try this build command :

cl /GZ /GX /Zi another.cpp pp.cpp

/JOlesen
 
Ahhhhh! It's so simple! So you have to include ALL source files. Thanks everyone!
 
I still don't understand how apatterno's example won't work. I have that same setup in my project, but I get the LNK2001 error. I've searched throughout this site, and I see the popular remedy is to include the .lib file in the project. I don't have a .lib file. Also, I'm compiling using the Green Hills MULTI IDE- not MS Visual Studio.

So far, I've gotten around it by either posting the implementation in the definition file or simply including the .cpp file instead of the .h file. Hmmmm.

I'm so mad that this remedial C++ problem has stumped me!
Please help. Thanks.
 
mj143: apatterno's example not included main() - it's assumed. LNK2001 informs about it. That's all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top