Undefined reference to 'Class::Function()'

Apr 16, 2016 at 10:49pm
Hello, friends. I have a problem with this code.
I'm noob in C++, it's for that I've some doubts about this.

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
#ifndef CONTACTO_H
#define CONTACTO_H
#include <iostream>
#include <string>

using namespace std;

class Contacto
{
    public:
        Contacto();
        Contacto(string,string,long);
        string Getnombre() { return nombre; }
        void Setnombre(string val) { nombre = val; }
        string Getcorreo() { return correo; }
        void Setcorreo(string val) { correo = val; }
        long Gettelefono() { return telefono; }
        void Settelefono(long val) { telefono = val; }
        void Imprimir();
    protected:

    private:
        string nombre;
        string correo;
        long telefono;
};

#endif // CONTACTO_H 


1
2
3
4
5
6
7
8
#include "Contacto.h"

Contacto::Contacto(string nombre, string correo = 0, long telefono = 0)
{
    Setnombre(nombre);
    Setcorreo(correo);
    Settelefono(telefono);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Contacto.h"

using namespace std;

int main()
{
    string nombre, correo;
    long telefono;
    int elementos = 2;
    Contacto *matDin = new Contacto[elementos];



    delete []matDin;
    return 0;
}[/output]


What is the error in the code? I need to create a dyanmic array of Contacto.
Last edited on Apr 16, 2016 at 11:19pm
Apr 17, 2016 at 12:06pm
What is the error in the code?

The error is:
undefined reference to `Contacto::Contacto()'



The class Contacto has two constructors,
11
12
    Contacto();
    Contacto(string,string,long);


When this line is executed,
 
    Contacto *matDin = new Contacto[elementos];

the default constructor is called for each of the Contacto objects created in the array.

Where is the code for the default constructor?

You could code it like this:
 
Contacto::Contacto() : telefono(0) { }


Apr 17, 2016 at 9:17pm
But if I put the mail and the phone like default, how should I do?
Apr 17, 2016 at 10:10pm
Default value of std::string is just an empty string "". If you want some other default value, you need to specify it.
Apr 17, 2016 at 10:16pm
Okay, man. Thank you for your answer!
Topic archived. No new replies allowed.