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

Already defined functions?

Status
Not open for further replies.

TimeOut

Programmer
Nov 21, 2000
16
GB
Hi all

I'm not sure if this question makes any sense, but I'm really stuck and had to ask...

I have some linking errors (mostly 2005) that I can't solve. One example is:

error LNK2005: "public: __thiscall play::play(void)" (??0play@@QAE@XZ) already defined in Ch10Ex3Doc.obj

Has anybody got any ideas on what to do? Would be really grateful for any help given. Thanks alot.

 
It OK! I've managed solve it (phew). Sorry for wasting your time.

Thanks
 
I have the same problem. Could you (or someone) please let me know how to solve it?

Thanks
 
declare function with extern everywhere except the place where you implement it. John Fill
1c.bmp


ivfmd@mail.md
 
Hmm...I have a similar problem, but I REALLY don't wanna use extern...the way I'm doing it is I have 3 pieces of source code and one headerfile that has some stuff in it....I want to include the .h file in all the source codes, but it tells me I can't redefine xxxxxx, which makes sense. I've tried the #ifndef...#endif trick, but it don't work. Can someone help me figgure out a way to include all these varibles/functions from my .h file into my 3 pieces of source code without using extern?
 
Valius: Does your header just have the function definitions in them? If so, you should write a serparate .h file for the function prototypes(this is the header that you include in all your .cpp files that need it) and a .cpp file for the function definitions. -> example

yourHeader.h -> will contain your function prototypes.

//Note: Enclose your prototypes with ->

#ifndef __YOURHEADER_H__
#define __YOURHEADER_H__

//Prototypes.

#endif

yourHeader.cpp -> will contain your function definitions.

#include "yourHeader.h" at the top of yourHeader.cpp.

For all the other .cpp files that need access to the functions, just #include "yourHeader.h"

Also did you let VC++ create your project for you? If so, make sure that any #includes are placed in "StdAfx.h". Then just #include "StdAfx.h" in all your .cpp files.

mlg400@blazemail.com
 
Aaaahhhh...I see, cool. Okay, I've seen talk about this sort of thing..just wasn't sure how to implement it. THANKS!!! I will give it a shot and see if it works for me. No, I didn't let VB do the project for me...I use the API (I'm curious how everything works under the MFC covers). Thanks SOOOOOO much for your input!

Niky Wiliams
J4 NTS Marketing, Inc.
 
Aaaahhhh...I see, cool. Okay, I've seen talk about this sort of thing..just wasn't sure how to implement it. THANKS!!! I will give it a shot and see if it works for me. No, I didn't let VB do the project for me...I use the API (I'm curious how everything works under the MFC covers). Thanks SOOOOOO much for your input!

Niky Williams
J4 NTS Marketing, Inc.
 
Oh, also, to answer your first question, no, my header file also has variables in it. What I did to get around this (which I really don't like) is make a "main" .h file...then have an "extern" .h file. in my main.cpp file, I include the main.h file that has all the variables in it...then in the exter.h file, I redefine all the variables with an extern keyword. That way in my other two .cpp files, I can just include the extern.h file an it seems to be okay with this. Kinda repeditive if you ask me...I can see mainainance problems down the road by taking this route. So, how could I do the same thing you suggested about the function prototypes, etc...with the variables. Would I use the same #ifndef....#endif idea and just enclose my variables in that block? Thanks again for all your help!

Niky Williams
J4 NTS Marketing, Inc.
 
Hmm...maybe I outta give you an Idea of what I'm doing..it may help describe my problem(s). I have a main program that puts a few controls on the window using the Win32 API. Well, I'm subclassing some edit boxes, but I want to stick the callback function in another .cpp file to seperate things a bit from the main program. So, I have one main file and two .cpp file that subclass two different edit boxes. I've defined my callback prototype for the edit boxes in my main .cpp file and that seems to work okay and it uses my callback fuctions instead of the default one. Well, I have (mainly) variables that both the main and the other two .cpp files use to share info. But when I try and include the .h file in the three .cpp files, thats when I get the multiple definition error. I hope this helps in describing my problem a bit better and again, I thank you so much for your help!

Niky Williams
J4 NTS Marketing, Inc
 
Was able to figgure something out...may help other people also...using the info I got from LiquidBinary (thanks) was able to come up with this. If you define your header like so...

#ifndef YOURHEADER_H
#define YOURHEADER_H
function prototypes
static variable
#endif

then you can include your .h file in any .cpp file and it should work. The main thing that was stumping me is that it screamed when I did multiple variable declarations...by defining them as "static" that seemed to solve the multiple declaration problem. Thanks again LiquidBinary for the info...hope this can help someone else out!

Niky Williams
J4 NTS Marketing, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top