Error compiling, help with POO in C ++

0

I am learning POO programming and I have a small mistake when trying to do my first "object". You give me this error.

  

C: \ Users \ Manflack \ AppData \ Local \ Temp \ ccf4P5ij.o poo3.cpp :(. text + 0x181): undefined reference to 'Person :: Person (int, int, int)'

This is my code:

#include<iostream>
using namespace std;
class Persona{
private:
int alt,pe,prommd; //alto peso promediomes
public:
Persona(int,int,int);
void asignAlt(int,int);
void cPe(int,int,int);
void promm(int);
void prnt();
};

void Persona::asignAlt(int mAlt,int pAlt){
alt=(mAlt+pAlt)/2; }
void Persona::cPe(int mPe,int pPe,int cd){
pe=((mPe+pPe)/2)+cd/100; }
void Persona::promm(int prom){
prommd=prom/30; }
void Persona::prnt(){
cout<<"La altura de la persona es de: "<<alt<<"\nSu peso es de: "<<pe<<"\nY su ingreso diario es de: "<<prommd; }

main(){
int t1,t2,t3;
cout<<"Ingrese la altura de su madre y su padre: ";
cin>>t1>>t2;
Persona Mariano(t1,t2,t3);
cout<<"Ingrese el peso de su madre, su padre y cuantas calorias consume al dia: ";
cin>>t1>>t2>>t3;
Mariano.cPe(t1,t2,t3);
cout<<"Ingrese su sueldo promedio al mes: "<<endl;
cin>>t1;
Mariano.promm(t1);
Mariano.prnt();

cin.get();
return 0; }
    
asked by Manflack 12.12.2016 в 02:01
source

3 answers

2

The error is clearly telling you:

undefined reference to Persona::Persona(int, int, int)

That line goes to say: " I can not find the definition of the function Persona with three parameters int in the context of Persona:: .

Problem

This can happen for two reasons:

  • The Persona::Persona(int, int, int) function is declared but not defined. In other words, the function exists but has no body (the body of the function is what goes between keys { and } .
  • The function Persona::Persona(int, int, int) is declared and defined but the compiler can not find the definition. Maybe the definition is in another code file that for some reason is not being compiled.
  • Solution

    Add the definition of the function Persona::Persona(int, int, int) (constructor) to your code; to make it " clearer " I would add it in the same position as the declaration, that is, before the function Persona::asignAlt :

    Persona::Persona(int altura, int peso, int promedio) :
        alt(altura), pe(peso), prommd(promedio) {}
    
    void Persona::asignAlt(int mAlt,int pAlt){
    alt=(mAlt+pAlt)/2; }
    void Persona::cPe(int mPe,int pPe,int cd){
    pe=((mPe+pPe)/2)+cd/100; }
    void Persona::promm(int prom){
    prommd=prom/30; }
    void Persona::prnt(){
    cout<<"La altura de la persona es de: "<<alt<<"\nSu peso es de: "<<pe<<"\nY su ingreso diario es de: "<<prommd; }
    
        
    answered by 12.12.2016 / 08:55
    source
    0

    The problem is with the constructor of your Persona class, since constructors and destructors do not carry a ; at the end.

    Persona{
        public:
            Persona(){}  // Contructor
            ~Persona(){} // Destructor
    };
    
        
    answered by 12.12.2016 в 02:17
    0
    Persona::Persona(int, int , int)
    {
    
    }
    

    Add this line the problem is that you have not added the constructor's statement.

        
    answered by 12.12.2016 в 02:15