Click here to Skip to main content
15,912,578 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to change window RTL in RUNTIME? Pin
Kamyar Souri13-Aug-06 7:21
Kamyar Souri13-Aug-06 7:21 
QuestionLNK2005 error - first program with classes Pin
senthryl13-Aug-06 4:21
senthryl13-Aug-06 4:21 
This is my first attempt at a program with classes, as I'm new to VC6. The program worked fine, until I decided to split it into multiple files with #include. I've looked for a solution but have only found answers about declaring global variables in a header file, which I don't think I've done.

Here's the code for my Animal class (this program doesn't really serve any purpose, I'm just trying to learn how to use classes).

Main.cpp:
#include <iostream>
#include "Animal.cpp"

int main()
{
	Animal Lucy(5.0);
	std::cout << "Lucy is ";
	std::cout << Lucy.GetAge();
	std::cout << " years old.\n";
	Lucy.Eat();
	Lucy.Sleep();
	Lucy.Speak();
	return 0;
}


Animal.cpp:
#include <iostream>
#include "Animal.h"

//This file continues to implement all of the methods in Animal.h, but it was trimmed for size.


Animal.h:
class Animal
{
public:
	//Constructors
	Animal();
	Animal(unsigned short int defaultAge);
	Animal(double defaultWeight);
	Animal(unsigned short int defaultAge, double defaultWeight);
	~Animal();

	//Accessors
	unsigned short int GetAge() const;
	double GetWeight() const;
	void SetAge(unsigned short int newAge);
	void SetWeight(double newWeight);

	//Methods
	void Eat() const;
	void Sleep() const;
	void Speak() const;

protected:
	//Protected methods
	void Init();

	//Data members
	unsigned short int* itsAge;
	double* itsWeight;
};


I receive the following errors when linking:
Animal.obj : error LNK2005: "protected: void __thiscall Animal::Init(void)" (?Init@Animal@@IAEXXZ) already defined in main.obj
Animal.obj : error LNK2005: "public: __thiscall Animal::Animal(void)" (??0Animal@@QAE@XZ) already defined in main.obj
Animal.obj : error LNK2005: "public: __thiscall Animal::Animal(unsigned short)" (??0Animal@@QAE@G@Z) already defined in main.obj
Animal.obj : error LNK2005: "public: __thiscall Animal::Animal(double)" (??0Animal@@QAE@N@Z) already defined in main.obj
Animal.obj : error LNK2005: "public: __thiscall Animal::Animal(unsigned short,double)" (??0Animal@@QAE@GN@Z) already defined in main.obj
Animal.obj : error LNK2005: "public: __thiscall Animal::~Animal(void)" (??1Animal@@QAE@XZ) already defined in main.obj
Animal.obj : error LNK2005: "public: unsigned short __thiscall Animal::GetAge(void)const " (?GetAge@Animal@@QBEGXZ) already defined in main.obj
Animal.obj : error LNK2005: "public: double __thiscall Animal::GetWeight(void)const " (?GetWeight@Animal@@QBENXZ) already defined in main.obj
Animal.obj : error LNK2005: "public: void __thiscall Animal::SetAge(unsigned short)" (?SetAge@Animal@@QAEXG@Z) already defined in main.obj
Animal.obj : error LNK2005: "public: void __thiscall Animal::SetWeight(double)" (?SetWeight@Animal@@QAEXN@Z) already defined in main.obj
Animal.obj : error LNK2005: "public: void __thiscall Animal::Eat(void)const " (?Eat@Animal@@QBEXXZ) already defined in main.obj
Animal.obj : error LNK2005: "public: void __thiscall Animal::Sleep(void)const " (?Sleep@Animal@@QBEXXZ) already defined in main.obj
Animal.obj : error LNK2005: "public: void __thiscall Animal::Speak(void)const " (?Speak@Animal@@QBEXXZ) already defined in main.obj
Debug/ClassTest.exe : fatal error LNK1169: one or more multiply defined symbols found


All of these files are included in the same workspace in the same directory.
AnswerRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 4:45
professionalChris Losinger13-Aug-06 4:45 
GeneralRe: LNK2005 error - first program with classes Pin
senthryl13-Aug-06 5:07
senthryl13-Aug-06 5:07 
GeneralRe: LNK2005 error - first program with classes [modified] Pin
Chris Losinger13-Aug-06 5:14
professionalChris Losinger13-Aug-06 5:14 
GeneralRe: LNK2005 error - first program with classes [modified] Pin
senthryl13-Aug-06 5:20
senthryl13-Aug-06 5:20 
GeneralRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 5:31
professionalChris Losinger13-Aug-06 5:31 
GeneralRe: LNK2005 error - first program with classes Pin
senthryl13-Aug-06 5:44
senthryl13-Aug-06 5:44 
GeneralRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 8:41
professionalChris Losinger13-Aug-06 8:41 
GeneralRe: LNK2005 error - first program with classes Pin
Mike Dimmick13-Aug-06 5:28
Mike Dimmick13-Aug-06 5:28 
QuestionGetting friend member functions to work Pin
Joel Becker13-Aug-06 2:11
Joel Becker13-Aug-06 2:11 
AnswerRe: Getting friend member functions to work Pin
Mike Dimmick13-Aug-06 5:30
Mike Dimmick13-Aug-06 5:30 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 7:13
Joel Becker13-Aug-06 7:13 
AnswerRe: Getting friend member functions to work Pin
Bartosz Bien13-Aug-06 11:11
Bartosz Bien13-Aug-06 11:11 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 11:22
Joel Becker13-Aug-06 11:22 
GeneralRe: Getting friend member functions to work [modified] Pin
Bartosz Bien13-Aug-06 11:47
Bartosz Bien13-Aug-06 11:47 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 12:22
Joel Becker13-Aug-06 12:22 
QuestionString problem [modified] Pin
73Zeppelin13-Aug-06 1:28
73Zeppelin13-Aug-06 1:28 
GeneralRe: String problem Pin
ovidiucucu13-Aug-06 2:40
ovidiucucu13-Aug-06 2:40 
QuestionHow to print the text in the new line of a multi line edit box . Pin
VCSharp00713-Aug-06 1:02
VCSharp00713-Aug-06 1:02 
AnswerRe: How to print the text in the new line of a multi line edit box . Pin
ovidiucucu13-Aug-06 2:44
ovidiucucu13-Aug-06 2:44 
GeneralRe: How to print the text in the new line of a multi line edit box . Pin
Bram van Kampen13-Aug-06 14:54
Bram van Kampen13-Aug-06 14:54 
QuestionProblem in using GDI+ Pin
Dhananjayak0212-Aug-06 23:44
Dhananjayak0212-Aug-06 23:44 
AnswerRe: Problem in using GDI+ Pin
Stick^13-Aug-06 0:38
Stick^13-Aug-06 0:38 
Questionplease help Pin
jitun_vcpp12-Aug-06 20:56
jitun_vcpp12-Aug-06 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.