User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 29 Apr 2005, 14:48   #1
Radical Edward
Registered User
 
Join Date: Feb 2002
Location: South Pacific
Posts: 4,911
Radical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriend
MFC linker problem

I am using visual C++ 6.0 here and I am having a problem with the linker, not sure why. here is what I am doing.

I am basically creationg the default MFC application, with SDI rather then MDI, and turning off printing. other than that, all things are default.

I create a simple header file called testclass.h which consists of the following:

class testclass
{
public:
testclass();
private:
int myInt;
};

testclass::testclass()
{
myInt = 1
}

////////////// that is all

then in the CmyDoc class I add the member variable

testclass theTest; // in the CmyDoc.cpp

and

#include "testclass.h" // in the CmyDoc.h file


and then compile. I get a LNK2005 error, saying I have already defined the .obj file

any idea why?

any help would be appreciated. ask me to describe things more if you need
__________________
I think it's time we blow this scene, get everybody and the stuff together..........

ok 3..... 2..... 1.. let's jam
Radical Edward is offline   Reply With Quote
Unread 29 Apr 2005, 15:17   #2
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: MFC linker problem

When you #include a function definition, you're effectively giving an implementation of that function each time. Every .c that #include's "testclass.h" - including those that #include "CmyDoc.h" - will redefine this function. I'll guess that "CmyDoc.h" is included from two different objects. Functions can only be implemented once or your linker will complain. However...

C++ does allow you to specify member functions in place, which will then be inlined wherever that function (or constructor) is used. Just keep in mind that this means more compilation, and might become worrisome when you use DLLs in the future.
class testclass
{
public:
testclass() { myInt = 1; }
private:
int myInt;
};

or:
testclass() : myInt (1) { }


The usual solution is to have a testclass.cpp, or just choose any one particular .cpp where you implement your function.

Last edited by queball; 29 Apr 2005 at 15:23.
queball is offline   Reply With Quote
Unread 29 Apr 2005, 17:43   #3
JetLinus
Friendly geek of GD :-/
 
JetLinus's Avatar
 
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
JetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud of
Arrow Re: MFC linker problem

Quote:
Originally Posted by queball
When you #include a function definition, you're effectively giving an implementation of that function each time. Every .c that #include's "testclass.h" - including those that #include "CmyDoc.h" - will redefine this function. I'll guess that "CmyDoc.h" is included from two different objects. Functions can only be implemented once or your linker will complain. However...
What about using (in the CmyDoc.h file):

Code:
#ifndef CMYDOC_H
#define CMYDOC_H

// stuff

#endif

If it should be #included multiple times, this stuff should do the trick, no?


I've only known C++ for 2 weeks now, but we were told to do this thing in every header file, and it made sense to me immediately!
__________________
[»] Entropy increases! :-/
JetLinus is offline   Reply With Quote
Unread 29 Apr 2005, 18:06   #4
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: MFC linker problem

I thought it was traditional to write class and method signatures in header files and their actual implementation in similarly named cpp files, though I don't do much C++
__________________
You're now playing ketchup
pablissimo is offline   Reply With Quote
Unread 30 Apr 2005, 02:31   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: MFC linker problem

Quote:
Originally Posted by JetLinus
What about using (in the CmyDoc.h file):

Code:
#ifndef CMYDOC_H
#define CMYDOC_H

// stuff

#endif

If it should be #included multiple times, this stuff should do the trick, no?


I've only known C++ for 2 weeks now, but we were told to do this thing in every header file, and it made sense to me immediately!
That's to stop something being defined twice when compiling a source file, not to stop something being extraneously implemented by each of two objects. Compiler not linker.
queball is offline   Reply With Quote
Unread 30 Apr 2005, 02:51   #6
JetLinus
Friendly geek of GD :-/
 
JetLinus's Avatar
 
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
JetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud of
Arrow Re: MFC linker problem

Hm, don't really understand that (yet).
You're the experts. So well. Hope you'll find something to work it out then.
I'm out of ideas (as I have no clue about it).
__________________
[»] Entropy increases! :-/
JetLinus is offline   Reply With Quote
Unread 1 May 2005, 11:06   #7
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: MFC linker problem

Quote:
Originally Posted by JetLinus
Hm, don't really understand that (yet).
Say you have compiler-error.c:
Code:
#include "useful.h"
#include "useful.h"
(In reality you'd include the same thing twice indirectly rather than directly like this but the result is the same.)

And useful.h:
Code:
typedef struct { } useful_type;
Then my compiler errors, with the slightly unhelpful message "conflicting types for `useful_type'". This can be solved using #define's as you suggest. It's a compilation issue, it's solved with a compilation tool, the preprocessor.




Now say you have c1.c and c2.c, each identical:
Code:
typedef struct { } useful_type;
void useful_function() { }
Each will compile fine. It doesn't matter that you define the same type twice if it's in different files. In fact, you generally have to do so. Types are instructions for the compiler, so you need them included in each source file you use them in.

But when you try to link c1.o and c2.o the linker will error "multiple definition of `useful_function'". This is a linking issue, so isn't something solved using #define's in the manner you describe.

This is all second nature if you use the convention pab describes: useful.c for definitions, useful.h for declarations. I just wanted to point out that inline functions do indeed go in headers.
queball is offline   Reply With Quote
Unread 1 May 2005, 12:22   #8
JetLinus
Friendly geek of GD :-/
 
JetLinus's Avatar
 
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
JetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud of
Arrow Re: MFC linker problem

Ah I see... Cheers!
__________________
[»] Entropy increases! :-/
JetLinus is offline   Reply With Quote
Unread 2 May 2005, 14:20   #9
Radical Edward
Registered User
 
Join Date: Feb 2002
Location: South Pacific
Posts: 4,911
Radical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriendRadical Edward needs a job and a girlfriend
Re: MFC linker problem

queball was right in this case. I stripped out all the implementation into cpp files and it works a treat now. thanks
__________________
I think it's time we blow this scene, get everybody and the stuff together..........

ok 3..... 2..... 1.. let's jam
Radical Edward is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 10:16.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018