GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Dec-2007, 22:50
SimonP SimonP is offline
New Member
 
Join Date: Dec 2007
Posts: 6
SimonP is on a distinguished road

Experiencing a linking error with MS Visual C++ 2005 Express Edition


Hi guys, I'm a highly experienced Java programmer who is just making the transition to C++. I look forward to becoming an active poster and contributing the GIDForums community.
I am in the process of experimenting with splitting a project into multiple files. I have a small project consisting of the following five files:

Cat.h:
CPP / C++ / C Code:
class Cat
{
public:
	Cat(int initialAge);
	~Cat();
	int getAge();
	void setAge(int age);
private:
	int age_;
};

stdafx.h:
CPP / C++ / C Code:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here
#include <iostream>

Cat.cpp:
CPP / C++ / C Code:
#include "stdafx.h"
#include "Cat.h"

Cat::Cat(int initialAge)
{
	age_ = initialAge;
}
Cat::~Cat()
{
	std::cout << "Cat Destructor\n"; //Do nothing
}
int Cat::getAge()
{
	return age_;
}
void Cat::setAge(int age)
{
	age_ = age;
}

CatTest.cpp:
CPP / C++ / C Code:
// CatTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Cat.cpp"

int _tmain(int argc, _TCHAR* argv[])
{
	Cat snowball(2);
	return 0;
}

stdafx.cpp:
CPP / C++ / C Code:
// stdafx.cpp : source file that includes just the standard includes
// CatTest.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

Unfortunately when I try to compile the above project I receive the following linking error:
Code:
1>------ Rebuild All started: Project: CatTest, Configuration: Debug Win32 ------ 1>Deleting intermediate and output files for project 'CatTest', configuration 'Debug|Win32' 1>Compiling... 1>stdafx.cpp 1>Compiling... 1>CatTest.cpp 1>Cat.cpp 1>Generating Code... 1>Compiling manifest to resources... 1>Linking... 1>Cat.obj : error LNK2005: "public: __thiscall Cat::Cat(int)" (??0Cat@@QAE@H@Z) already defined in CatTest.obj 1>Cat.obj : error LNK2005: "public: __thiscall Cat::~Cat(void)" (??1Cat@@QAE@XZ) already defined in CatTest.obj 1>Cat.obj : error LNK2005: "public: int __thiscall Cat::getAge(void)" (?getAge@Cat@@QAEHXZ) already defined in CatTest.obj 1>Cat.obj : error LNK2005: "public: void __thiscall Cat::setAge(int)" (?setAge@Cat@@QAEXH@Z) already defined in CatTest.obj 1>C:\Users\Simon\Documents\Visual Studio 2005\Projects\CatTest\Debug\CatTest.exe : fatal error LNK1169: one or more multiply defined symbols found 1>Build log was saved at "file://c:\Users\Simon\Documents\Visual Studio 2005\Projects\CatTest\CatTest\Debug\BuildLog.htm" 1>CatTest - 5 error(s), 0 warning(s) ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Does anyone have any suggestions as to how I might be able to fix this problem?

Sincerely,

Simon Pearson
  #2  
Old 19-Dec-2007, 23:05
SimonP SimonP is offline
New Member
 
Join Date: Dec 2007
Posts: 6
SimonP is on a distinguished road

Re: Experiencing a linking error with MS Visual C++ 2005 Express Edition


Apologies for asking such a newb question.
  #3  
Old 20-Dec-2007, 07:01
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 924
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Experiencing a linking error with MS Visual C++ 2005 Express Edition


In CatTest...

change this:
CPP / C++ / C Code:
#include "Cat.cpp"
to this:
CPP / C++ / C Code:
#include "Cat.h"
as only the class definition is needed for the test file, not the implementation.

Note the function calls in the linker errors:
Quote:
Originally Posted by SimonP
1>Cat.obj : error LNK2005: "public: __thiscall Cat::Cat(int)" (??0Cat@@QAE@H@Z) already defined in CatTest.obj
1>Cat.obj : error LNK2005: "public: __thiscall Cat::~Cat(void)" (??1Cat@@QAE@XZ) already defined in CatTest.obj
1>Cat.obj : error LNK2005: "public: int __thiscall Cat::getAge(void)" (?getAge@Cat@@QAEHXZ) already defined in CatTest.obj
1>Cat.obj : error LNK2005: "public: void __thiscall Cat::setAge(int)" (?setAge@Cat@@QAEXH@Z) already defined in CatTest.obj
The Cat.obj has those functions (after compiling Cat.cpp), and CatTest.obj has them too since it #include'd Cat.cpp.

EDIT:
Also, consider adding this small change to all header files from now on, as it will prevent 'redefinition' problems down the road: (this isn't happening now, just looking ahead)
Quote:
A standard for class header files
In each header file that contains a class, you should first check to see if the file has already been included in this particular code file. You do this by checking a preprocessor flag. If the flag isn’t set, the file wasn’t included and you should set the flag (so the class can’t get re-declared) and declare the class. If the flag was set the class has already been declared so you should just ignore the code declaring the class. Here’s how the header file should look:

#ifndef CLASS_FLAG
#define CLASS_FLAG
// Class declaration here...
#endif // CLASS_FLAG
So this change would make your header file look like this:
CPP / C++ / C Code:
#ifndef CAT_H
#define CAT_H

class Cat
{
public:
	Cat(int initialAge);
	~Cat();
	int getAge();
	void setAge(int age);
private:
	int age_;
};

#endif // CAT_H 
HTH
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 20-Dec-2007, 14:34
SimonP SimonP is offline
New Member
 
Join Date: Dec 2007
Posts: 6
SimonP is on a distinguished road

Re: Experiencing a linking error with MS Visual C++ 2005 Express Edition


TurboPT, many thanks for offering such a detailed response, I made the changes that you suggested and it all compiles fine now. Thanks for the help.
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
can i run visual 2005 mfc project in visual 6 or 2003? spillover MS Visual C++ / MFC Forum 1 27-Dec-2006 18:09
How Visual C++ only from Visual Studio .net Picstudent .NET Forum 2 28-May-2006 20:52
What is "Ambigious symbol" ??*( a compilation error) small_ticket CPP / C++ Forum 2 07-Jan-2005 21:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.