Advertisement

VC++ 6.0 Link Error (off topic)

Started by January 10, 2004 10:42 PM
3 comments, last by Devil God 21 years, 1 month ago
I write a box class myself. I only created a win32 console project, and I put header in box.h, definition in box.cpp, and test.cpp to use the object. It got compiled, but it gave me errors when I try to run the debug. /**************************** test.obj : error LNK2005: "public: __thiscall Box::Box(void)" (??0Box@@QAE@XZ) already defined in box.obj test.obj : error LNK2005: "public: __thiscall Box::Box(double,double,double)" (??0Box@@QAE@NNN@Z) already defined in box.obj test.obj : error LNK2005: "public: double __thiscall Box::surface(void)const " (?surface@Box@@QBENXZ) already defined in box.obj test.obj : error LNK2005: "public: double __thiscall Box::volume(void)const " (?volume@Box@@QBENXZ) already defined in box.obj test.obj : error LNK2005: "public: int __thiscall Box::compareVolume(class Box const &)const " (?compareVolume@Box@@QBEHABV1@@Z) already defined in box.obj test.obj : error LNK2005: "public: int __thiscall Box::compareSurface(class Box const &)const " (?compareSurface@Box@@QBEHABV1@@Z) already defined in box.obj Debug/oo.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. *************************/ All those "already defined" errors are cause by my member functions of the class. I checked on Microsoft''s website and searched on google. Some say that''s caused by MFC linking problem if I try to run a MFC program, but mine is not. Some say I should use "extern" to declare variables, but mine is a class, so can''t use "extern". I don''t know how I can''t fix this $h*t. Many thx for any help DG
didnt you forget the #ifndef ... #define ... #endif pattern in your header file? it looks like your header is inserted into the code more than once...
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Advertisement
I did put the preprocessors in my header, but it still gives the problem.

// box.h
#ifndef BOX_H
#define BOX_H

class box
{
// my declarations
};
#endif



// box.cpp
#include <iostream.h> // need the .h in VC compiler
#include "box.h"

// my definitions



I guess MSVS developer is somehow different from ISO standard.

DG
Maybe this topic has what you're looking for.
EDIT: else, mayb you should place only the class declaration in the header, the rest in a seperate source file (if class declarations are valid, i never used them ).

[edited by - Tree Penguin on January 11, 2004 6:52:02 PM]
Thx for the topic, orbano, but I figured out the problem by reading Ivor Horton's "Beginning Visual C++ 6", and I recommend you remember this if you will ever use VC compiler.

I originally placed my box declaration in header file, box definition in cpp file, and another file that test my box class.

That is what most oo programs are implemented, but somehow visual c++ compiler just give me linking error when I tried to debug my program.

Ivor Horton's book taught me that VC use different class declaration and data member declaration.

It's actually not much different, all I have to do is putting a small/capitol c in front of my class name and m_ in front of my data members.

Example: From class box ---> class cbox
From double length ---> double m_length

That's all! Maybe that's the way that VC compiler need to regonize the class declaration, but I still think it's bullshit cuz that freakin' LNK2005 linking error has troubled a lot of people.

Thx for all your replies anyway

DG

[edited by - Devil God on January 11, 2004 8:00:32 PM]

This topic is closed to new replies.

Advertisement