0

This error pops when I try to compile this (sorry its in spanish)

#include <iostream>
#include <string>

using namespace std;

//////////////////////////////
// Contacto

class contacto
{
    private:
        string nombre;
        string apellido;
        string numTelf;
        string numCel;
        string correo;
        string ciudad;
        string pais;
        string grupo;
    public:
        contacto (string nom, string apel, string tel, string cel, string cor, string ciu, string pai, string grup)
        {
        nombre = nom;
        apellido = apel;
        numTelf = tel;
        numCel = cel;
        correo = cor;
        ciudad = ciu;
        pais = pai;
        grupo = grup;       
        }
        void getContacto (string nom, string apel)
        {
            
        }
};

class agenda
{
    private:
        contacto arreglo[40];
    public:
    agenda();
        void setContacto(int n)
        {
        }
};



int main ()
{
int op, N;
agenda agen;

    cout<<"N:";
    cin>>N;
    agen.setContacto(N);        
    
    
system("pause");
return 0;
}

I know it has something to do with the object declaration and the constructor of the agenda class, ive tried to erase it but ill just give me other errors, I just need to access the agend.setContacto(N); but it keeps giving me errors, and as the Agenda class only has an array of objects, I dont know how to make a valid constructor.

Ill leave the classes out here to make it easier to see:

class contacto:

class contacto
{
    private:
        string nombre;
        string apellido;
        string numTelf;
        string numCel;
        string correo;
        string ciudad;
        string pais;
        string grupo;
    public:
        contacto (string nom, string apel, string tel, string cel, string cor, string ciu, string pai, string grup)
        {
            nombre = nom;
            apellido = apel;
            numTelf = tel;
            numCel = cel;
            correo = cor;
            ciudad = ciu;
            pais = pai;
            grupo = grup;
        }
        void setContacto (string nom, string apel, string tel, string cel, string cor, string ciu, string pai, string grup)
        {
            nombre = nom;
            apellido = apel;
            numTelf = tel;
            numCel = cel;
            correo = cor;
            ciudad = ciu;
            pais = pai;
            grupo = grup;           
        }
        void getContacto (string nom, string apel)
        {
            
        }
};

class agenda:

class agenda
{
    private:
        contacto arreglo[40];
    public:
    agenda();
        void setContacto(int n)
        {
};

EDIT: The error that pops is

[Linker error] undefined reference to `agenda::agenda()'

ld returned 1 exit status

EDIT2: The contacto class just needed an empty constructor for the Agenda class to initialize the array

Community
  • 1
  • 1
Tronikart
  • 113
  • 5
  • 2
    Surely there was more to it than that message, and *surely* this is not an [MCVE](http://stackoverflow.com/help/mcve). – chris Mar 12 '15 at 03:03
  • 1
    Unless we can compile the code, we cannot reproduce the error. You have not shown us exactly the error. It would be better to show the error in Spanish than not to show it at all. If you can compile with LANG=en_us set in the environment temporarily (or otherwise make the compiler work in English for long enough to ask this question), it might give you better reponses. It would be a good idea to identify the platform. I suspect it is a Linux variant, but that's a guess, and far from a certainty. You need to help us to help you. – Jonathan Leffler Mar 12 '15 at 03:10
  • RIGHT! Sorry, Im super tired and forgot about that, the exact error is "[Linker error] undefined reference to `agenda::agenda()', ld returned 1 exit status" – Tronikart Mar 12 '15 at 03:20
  • @Tronikart, You might want to take a look at http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Mar 12 '15 at 03:21
  • and did you implement `agenda::agenda()`? maybe try to add `{}` after the constructor agenda or remove the declaration... – SHR Mar 12 '15 at 03:22
  • where would I do that? I just got into classes this monday and I feel they didnt tell me everything they had to. Im looking into the link chris posted EDIT: If I remove the declaration a "no matching function for call to `agenda::agenda()'" error pops in with "agenda::agenda(const agenda&)" as sugested cadidate – Tronikart Mar 12 '15 at 03:26
  • in the 6th row in `class agenda` right after the `public:`, you declared empty constructor, but didn't implemented it. to implement you write it like this: `agenda(){}` instead of `agenda();`. to use the default constructor just remove or comment the line `agenda();`. – SHR Mar 12 '15 at 03:30
  • If I remove the declaration a `no matching function for call to agenda::agenda()` error pops in with `agenda::agenda(const agenda&)` as sugested cadidate, if I set it as `agenda(){}` a `no matching function for call to `contacto::contacto()'` error shows – Tronikart Mar 12 '15 at 03:34

1 Answers1

0

When you declare one constructor, you should also declare the default (empty) constructor.

In class contacto, you must have an empty constructor in manner to declare the array contacto arreglo[40]; in class agenda.

Therefore, add the following to the public section in class contacto :

contacto(){
/*you can init members...*/  

}

In class agenda, if you stated a function and used it, you must implement it. The default constructor used from main in the line agenda agen;

So if you declare it, you must put {} to implement it. if you not declare, then you've got the default since there is no other constructor.

Saladom
  • 11
  • 5
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Thanks a lot man! I thought that this `contacto (string nom, string apel, string tel, string cel, string cor, string ciu, string pai, string grup)` would work as it, but it turns out that it works as the parameterized constructor, and I actually needed the empty one. It works now, I can get some sleep now – Tronikart Mar 12 '15 at 04:10