Indefinite reference in C++

Asked

Viewed 533 times

0

I’m studying c++ and found a problem when working with object orientation, I followed some tutorials but it didn’t work.

Bill. h

#ifndef CONTA_H
#define CONTA_H

#include <string>

using namespace std;

class Conta{

    private:
        string nome;
        int idade;

    public:
        string getNome();
        int getIdade();
};

#endif // CONTA_H

Cpp account.

#include "Conta.h"
#include <string>

using namespace std;

string Conta::getNome(){
    return nome;
}

int Conta::getIdade(){
    return idade;
}

main.cpp

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

using namespace std;

int main()
{
    Conta p;

    cout << p.getNome() << endl;
    return 0;
}

When I compile the code it brings me this error.

undefined reference to `Conta::getNome()'

Note: my ide is Codeblocks.

  • 3

    Probably not compiling the Conta.cpp along.

  • i did a include on my main.cpp ( #include "Account.cpp" ) it worked, but doing it is good practice or is wrong?

  • Don’t do this. Working doesn’t mean it’s right.

  • Thanks bigown, the problem was at compilation time same, I installed the dev c++ and ran first.

  • Now you have an extra problem :D

  • but I won’t use it no :) I’m testing other compilers

Show 1 more comment

1 answer

2

This is not a good way to solve this problem because every time you make a change to the.cpp account file, you will need to recompile the main.cpp file.

The best would be to hit the way it is compiling so it generates all the necessary objects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.