Author Topic: Im having problems with headers and cpp files :)  (Read 5602 times)

Offline ace4dsl

  • Single posting newcomer
  • *
  • Posts: 5
Im having problems with headers and cpp files :)
« 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


Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Im having problems with headers and cpp files :)
« Reply #1 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ace4dsl

  • Single posting newcomer
  • *
  • Posts: 5
Re: Im having problems with headers and cpp files :)
« Reply #2 on: July 02, 2009, 08:41:01 am »
that is what i did..

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Im having problems with headers and cpp files :)
« Reply #3 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ace4dsl

  • Single posting newcomer
  • *
  • Posts: 5
Re: Im having problems with headers and cpp files :)
« Reply #4 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,

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Im having problems with headers and cpp files :)
« Reply #5 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ