Code::Blocks Forums

User forums => Help => Topic started by: ace4dsl on July 02, 2009, 08:32:56 am

Title: Im having problems with headers and cpp files :)
Post by: ace4dsl on July 02, 2009, 08:32:56 am
hi for some reason when i add a header file and use it with cpp files I always always always :'( get those errors:

||=== personTry, Debug ===|
obj\Debug\main.o||In function `main':|
C:\Documents and Settings\Administrator\Desktop\cpp\personTry\main.cpp|10|undefined reference to `Person::Person(std::string)'|
C:\Documents and Settings\Administrator\Desktop\cpp\personTry\main.cpp|13|undefined reference to `Person::getIsName()'|
||=== Build finished: 2 errors, 0 warnings ===|

it might be the code so il give you the code also:

main :
Code
#include <iostream>
#include <string>

#include "Person.h"

using namespace std;

int main()
{
    Person p("Lebee");


    string s = p.getIsName();
    cout << s;

    return 0;
}


cpp file :
Code
#include <iostream>
#include <string>

#include "Person.h"

using namespace std;

Person::Person()
{
    m_name = "david";
}

Person::Person(string p_name)
{
    m_name = p_name;
}

string Person::getIsName()
{
   return m_name;
}


header file:
Code
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED

#include <string>

class Person
{
  public:

  Person();
  Person(std::string p_name);
  std::string getIsName();

  private:

  std::string m_name;

};

#endif // PERSON_H_INCLUDED

Title: Re: Im having problems with headers and cpp files :)
Post by: MortenMacFly on July 02, 2009, 08:38:12 am
it might be the code so il give you the code also:
You need to create a project, then add all 3 files (2x *.cpp and 1x *.h) to it and hit compile.
Title: Re: Im having problems with headers and cpp files :)
Post by: ace4dsl on July 02, 2009, 08:41:01 am
that is what i did..
Title: Re: Im having problems with headers and cpp files :)
Post by: MortenMacFly on July 02, 2009, 08:53:52 am
that is what i did..
Switch on full compile log, hit re-build and post the log here (see my sig).
Notice: If you have several targets you'll need to enable *all* files for each target in the project options. This will ensure Person.cpp gets compiled and linked what is missing in your case. This is definitely a wrong project setup.
Title: Re: Im having problems with headers and cpp files :)
Post by: ace4dsl on July 02, 2009, 06:21:09 pm
ok mayb eim doing this wrong, lets say i have my main already i want to add a header file what are the best steps to do it to make sure its well linked, thanks in advance,
Title: Re: Im having problems with headers and cpp files :)
Post by: MortenMacFly on July 06, 2009, 08:04:10 am
want to add a header file what are the best steps to do it to make sure its well linked
Header files are not linked at all! Grab yourself a C++ for beginners book and start reading. Topic locked as we are no C/C++ forum.