1

I have looked around for over an hour now and I cannot find anything that is benefiting my situation. I am a beginner at C++, and this is my first attempt at splitting my code into two .cpp files and one .h file. I have yet to encounter any success. Here is my code :

#include "stdafx.h"
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
   Cat Frisky; // declare variable of type Cat, creates an object
   Frisky.SetAge( 5 ); // put value INTO object
   Frisky.Meow( ); // make object do something
   std::cout << "Frisky is a cat who is " ;
   std::cout << Frisky.GetAge() << " years old.\n " ;
   Frisky.Meow( ); // make object do something
   return 0;
}

// This is my cat.h
class Cat {

public:
   int GetAge();                        // accessor
   void SetAge( int age ); // accessor
   void Meow();         // general function

private:
   int itsAge;     // member variable

};

#include "Cat.h"
int Cat::GetAge( )
{
   return itsAge;

} // end function GetAge

// set function, PUT VALUE IN to the object
void Cat::SetAge( int age )
{
   itsAge = age ;

} // end function SetAge

// What do Cats do?

// What action should a Cat object perform?
void Cat::Meow( )
{
   std::cout << "Meow.\n";

} // end function Meow

and here is the error messages I have been encountering for over an hour now.

1>------ Build started: Project: TestCat, Configuration: Debug Win32 ------
1>  TestCat.cpp
1>TestCat.obj : error LNK2019: unresolved external symbol "public: int __thiscall Cat::GetAge(void)" (?GetAge@Cat@@QAEHXZ) referenced in function _main
1>TestCat.obj : error LNK2019: unresolved external symbol "public: void __thiscall Cat::Meow(void)" (?Meow@Cat@@QAEXXZ) referenced in function _main
1>TestCat.obj : error LNK2019: unresolved external symbol "public: void __thiscall Cat::SetAge(int)" (?SetAge@Cat@@QAEXH@Z) referenced in function _main
1>C:\Documents and Settings\Geena\Desktop\TestCat\Debug\TestCat.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have my Cat.cpp file and my Cat.h file put into the following directory : C:\Documents and Settings\Geena\Desktop\TestCat\TestCat

Just looking for the answer to get this damn program up and running already.

Thank You

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Dolbyover
  • 89
  • 1
  • 2
  • 10
  • 1
    1. Use include guards in headers. 2. The problem and solution are most likely included in [this](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – chris Feb 21 '13 at 06:16
  • 4
    why only TestCat.cpp compilling ? are there 3 files in your project ? [MyCat.h, MyCat.cpp, TestCat.cpp]. I think you just have to Add->Existing Item.. MyCat.cpp into your TestCat project. – user928204 Feb 21 '13 at 06:21
  • What do you mean by using include guards on my headers? – Dolbyover Feb 21 '13 at 06:21
  • 3
    @ZachKenny: [include guards](https://www.google.com/#hl=en&safe=off&output=search&sclient=psy-ab&q=include+guard) – moswald Feb 21 '13 at 06:23
  • Alright I looked further into include guards but I am not seeing how they would benefit my code, or even how to use them in my code. Sucks being a beginner. – Dolbyover Feb 21 '13 at 06:32
  • Simple, just add [`#pragma once`](http://en.wikipedia.org/wiki/Pragma_once#Example) to the top of your `.h` files. – congusbongus Feb 21 '13 at 06:42
  • Project->Add Existing Item... myCat.cpp – user928204 Feb 21 '13 at 06:45
  • I tried the #pragma once at the top of my only .h file and nothing has changed. Same errors. – Dolbyover Feb 21 '13 at 06:46
  • Tried adding Cat.cpp to my TestCat.cpp and it says that it is already there. I manually did it before, was not aware that I could do it from within Microsoft Visual. – Dolbyover Feb 21 '13 at 07:04
  • And.. *never* add .cpp files to .cpp files. Either define them correctly and include them properly in the build system you're using, or merge their code. Regarding the header files everyone is losing their jewels on, thats not your issue (but it is *an* issue). See the link chris posted at the very top of this comment list. If it is easier to add a file to a build than how MSDEV does it I don't see how. (ok, maybe vi and a Makefile). – WhozCraig Feb 21 '13 at 07:55
  • +1 for the question title :-) – Martin Ba Feb 22 '13 at 10:12

1 Answers1

1

I'm strongly suggesting you to fallow this tutorial before next question

cat.h

class Cat {
public:
    int GetAge();                      
    void SetAge( int age ); 
    void Meow(); 
private:
    int itsAge;  
};

cat.cpp

#include "cat.h"
#include <iostream>

using namespace std;

int Cat::GetAge(){return itsAge;}
void Cat::SetAge(int age){itsAge = age ;}
void Cat::Meow(){cout << "Meow.\n";} 

main.cpp

#include "cat.h"
#include <iostream>

using namespace std;

int main()
{
   Cat Frisky; 
   Frisky.SetAge(5);
   Frisky.Meow(); 
   cout << "Frisky is a cat who is " ;
   cout << Frisky.GetAge() << " years old.\n " ;
   Frisky.Meow( );
   system("pause");
   return 0;
}
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147