Help with error!

i am learning how to do this http://www.youtube.com/watch?v=NTip15BHVZc&feature=autoplay&list=ECAE85DE8440AA6B83&playnext=1 but i get an error whenever i do it.

main.cpp
#include "Information.h"
#include <string>
#include <iostream>

using namespace std ;




int main()
{
    Informations userObject ;
    userObject.setUsersName() ;
    userObject.PrintName() ;
    userObject.setAge() ;
    userObject.PrintAge() ;

    return 0;
} 


Information.h

#ifndef INFORMATION_H
#define INFORMATION_H
#include <string>
using namespace std;
using std::string;

class Informations
{
    public:

        void setUsersName() ;
        void setAge() ;
        string getUsersName() ;
        int getAge() ;
        void PrintName() ;
        void PrintAge() ;

    private:
        int x, age ;
        string nameofuser ;


} ;

#endif // INFORMATION _H 


Information.cpp

#include "Information.h"
#include <string>
#include <iostream>


using namespace std ;

class Informations{
    public:


        void setAge(){
            cout << "What year were you born in?" << endl ;
            cin >> x ;
            age = 2012 - x ;
        }

        int getAge(){
            return age ;
        }

        void PrintAge() {
            cout << "You are " << age - 1 << "/" << age << " years old." << endl ;
        }

        void setUsersName(){
            cout << "What is your name?" << endl ;
            cin >> nameofuser ;
        }

        string getUsersName(){
            return nameofuser ;
        }

        void PrintName(){
            cout <<"Hello " << nameofuser << "." << endl ;
        }


    private:
        string nameofuser ;
        int x, age ;
} ; 




but i get this error code


 obj\Debug\main.o||In function `main':|
E:\C++ Programs\My First Program\main.cpp|13|undefined reference to `Informations::setUsersName()'|
E:\C++ Programs\My First Program\main.cpp|14|undefined reference to `Informations::PrintName()'|
E:\C++ Programs\My First Program\main.cpp|15|undefined reference to `Informations::setAge()'|
E:\C++ Programs\My First Program\main.cpp|16|undefined reference to `Informations::PrintAge()'|
||=== Build finished: 4 errors, 0 warnings ===| 


Help please!
Last edited on
are they all in the same folder
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Information.h"
#include <string>
#include <iostream>

using namespace std ;

int main()
{
    Informations userObject ;
    userObject.setUsersName() ;
    userObject.PrintName() ;
    userObject.setAge() ;
    userObject.PrintAge() ;

    return 0;
}



Information.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include "Information.h"
#include <string>
#include <iostream>


        void Informations::setAge(){
            cout << "What year were you born in?" << endl ;
            cin >> x ;
            age = 2012 - x ;
        }

        int Informations::getAge(){
            return age ;
        }

        void Informations::PrintAge() {
            cout << "You are " << age - 1 << "/" << age << " years old." << endl ;
        }

        void Informations::setUsersName(){
            cout << "What is your name?" << endl ;
            cin >> nameofuser ;
        }

        string Informations::getUsersName(){
            return nameofuser ;
        }

        void Informations::PrintName(){
            cout <<"Hello " << nameofuser << "." << endl ;
        }



Information.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef INFORMATION_H
#define INFORMATION_H
#include <string>
using namespace std;
using std::string;

class Informations
{
    public:

        void setUsersName() ;
        void setAge() ;
        string getUsersName() ;
        int getAge() ;
        void PrintName() ;
        void PrintAge() ;

    private:
        int x, age ;
        string nameofuser ;


} ;

#endif // INFORMATION _H


When you are going to define class methods and functions in a headerfile, you have to tell from which class you should define it.

e.g:

void Informations::setAge(){
cout << "What year were you born in?" << endl ;
cin >> x ;
age = 2012 - x ;
}

Here are you telling that you are going to define the setAge() method from the Informations class.

And you don't have to use the using namespace std in the cpp file, because it inherits the namespace from the header file.

Good luck! :D
Last edited on
I get the same error even though i did what SuperHappy told me, also they are in the same directory.
You need to compile all the source files and link it all together. If you are using an IDE it often do this automatically if you make sure to put all the files in the same project.
Im using codeblocks IDE which should do that automatically. Also are there any other tutorials how to put classes in separate files?
Try to follow the video again, if the program still isn't working, go and look in the CodeBlocks forum : http://forums.codeblocks.org/

Maybe one of the settings is incorrect.


Happy Programming! :D
Last edited on
Topic archived. No new replies allowed.