Original Post
Hello, Im trying to split my code up into to diff .cpp files. For some reason, I get these errors. Sorry if this is a dumb question Thanks for any help errors im getting pet.cpp main.cpp
1>pet.obj : error LNK2005: "void __cdecl Pause(void)" (?Pause@@YAXXZ) already defined in main.obj
1>pet.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Pet::SetMood(void)" (?SetMood@Pet@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in main.obj
1>pet.obj : error LNK2005: "public: void __thiscall Pet::CheckStats(void)" (?CheckStats@Pet@@QAEXXZ) already defined in main.obj
1>pet.obj : error LNK2005: "private: void __thiscall Pet::PassTime(int)" (?PassTime@Pet@@AAEXH@Z) already defined in main.obj
1>pet.obj : error LNK2005: "public: void __thiscall Pet::Talk(int)" (?Talk@Pet@@QAEXH@Z) already defined in main.obj
1>pet.obj : error LNK2005: "public: void __thiscall Pet::Feed(int)" (?Feed@Pet@@QAEXH@Z) already defined in main.obj
1>pet.obj : error LNK2005: "public: void __thiscall Pet::Play(int)" (?Play@Pet@@QAEXH@Z) already defined in main.obj
1>pet.obj : error LNK2005: "public: void __thiscall Pet::Stats(void)" (?Stats@Pet@@QAEXXZ) already defined in main.obj
1>C:\Documents and Settings\Brandon\Desktop\Stuff\Programming\Book Excersises\Ch 8 - Classes\Pet Caretaker\Debug\Pet Caretaker.exe : fatal error LNK1169: one or more multiply defined symbols found
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//////////////////////////////////////////////////////////////////////////////////////
//
// CLASS PET
//
//////////////////////////////////////////////////////////////////////////////////////
class Pet
{
public:
Pet(int hunger, int lazy, int boredom): m_hunger(hunger), m_boredom(boredom), m_lazy(lazy)
{}
void Talk(const int bored = 3);
void Feed(const int food = 4);
void Play(const int activity = 4);
void Stats();
void CheckStats();
std::string SetMood();
private:
int m_hunger;
int m_boredom;
int m_lazy;
int m_mood;
void PassTime(const int time = 2);
};
//////////////////////////////////////////////////////////////////////////////////////
//
// GLOBAL FUNCTION DEFINITION
//
//////////////////////////////////////////////////////////////////////////////////////
void Pause()
{
std::cout << "\n\nPress ENTER to continue.";
std::cin.ignore();
std::cin.get();
}
//////////////////////////////////////////////////////////////////////////////////////
//
// CLASS PET FUNCTION DEFINITIONS
//
//////////////////////////////////////////////////////////////////////////////////////
std::string Pet::SetMood()
{
m_mood = (m_boredom + m_lazy + m_hunger);
std::string mood;
if(m_mood >= 15)
mood = "Happy";
else if(m_mood >= 10)
mood = "Fine";
else if(m_mood >= 5)
mood = "Sad";
else if(m_mood < 5)
mood = "Angry";
else if(m_mood <= 0) {
m_mood = 0;
mood = "Sad and Angry";
}
return mood;
}
void Pet::CheckStats()
{
if(m_hunger < 0)
m_hunger = 0;
if(m_boredom < 0)
m_boredom = 0;
if(m_lazy < 0)
m_lazy = 0;
}
void Pet::PassTime(const int time)
{
m_hunger -= time;
m_boredom -= time;
m_lazy -= time;
}
void Pet::Talk(const int bored)
{
std::vector<std::string> phrase;
phrase.push_back("Pet: Bark Bark Baaaark!!!");
phrase.push_back("Pet: Arrrrrrr!!!");
phrase.push_back("Pet: Raaaaawwr!!!!");
phrase.push_back("Pet: Ahhhhwwwww!!!");
// Gets random phrase for pet to say to the user
random_shuffle(phrase.begin(), phrase.end());
std::cout << "\n" << phrase[0];
m_boredom += bored;
PassTime();
Pause();
}
void Pet::Feed(const int food)
{
std::cout << "\nPet: Buuuuuurp!!!";
m_hunger += food;
PassTime();
Pause();
}
void Pet::Play(const int activity)
{
std::cout << "\nPet: Weeeeeeeeee! Bark! :)";
m_lazy += activity;
PassTime();
Pause();
}
void Pet::Stats()
{
system("cls");
CheckStats();
std::string current_mood = SetMood();
std::cout << "Pat Caretaker Stats:\n"
<< "\nHunger Level = " << m_hunger
<< "\nPlayful Level = " << m_lazy
<< "\nBoredom Level = " << m_boredom
<< "\n\nOverall Mood: " << current_mood
<< "\n\nHint: The Higher the Level, the Happy your Pet is.";
Pause();
}
#include "pet.cpp"
int main()
{
Pet pet(10,10,10);
int menu;
bool quit = false;
while(!quit)
{
system("cls");
std::cout << "Welcome to the Pet Caretaking Center!\n"
<< "Programmed by: Brandon Wall\n\n"
<< "1 = Listen to your pet\n"
<< "2 = Feed your pet\n"
<< "3 = Play with your pet\n"
<< "4 = See Pet's Stats\n"
<< "5 = quit\n\n"
<< "Selection: ";
std::cin >> menu;
switch (menu)
{
case 1:
pet.Talk();
break;
case 2:
pet.Feed();
break;
case 3:
pet.Play();
break;
case 4:
pet.Stats();
break;
case 5:
quit = true;
break;
default:
std::cout << "\nSorry, " << menu << " is an invalid option.";
Pause();
}
}
return 0;
}