Click here to Skip to main content
15,888,527 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Can a function call a afx_msg function??? Pin
mimimimilaw13-Aug-06 20:18
mimimimilaw13-Aug-06 20:18 
GeneralRe: Can a function call a afx_msg function??? Pin
Hamid_RT13-Aug-06 21:26
Hamid_RT13-Aug-06 21:26 
AnswerRe: Can a function call a afx_msg function??? Pin
prasad_som13-Aug-06 19:37
prasad_som13-Aug-06 19:37 
QuestionCan't Minimize windows Pin
Max++13-Aug-06 14:59
Max++13-Aug-06 14:59 
AnswerRe: Can't Minimize windows Pin
Waldermort13-Aug-06 15:04
Waldermort13-Aug-06 15:04 
Questionversioninfo problems Pin
Waldermort13-Aug-06 10:29
Waldermort13-Aug-06 10:29 
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 

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.