[C++]Problem with constructor

Hello, im manuel, new on this forum and new with c++. im having some problems with a project, hope someone can help me:

Usuario::Usuario() its call at "template<class T> class Nodo" line 14 but i dont know why. I have not even creat that constructor so this problem appears:

C:\Documents and Settings\user\Escritorio\TP1\scr\Nodo.h no matching function for call to `Usuario::Usuario()'


here is the code:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Usuario {
 
    private:
 
        string nombre;
 
        Lista<string>* amigos;
 
        Lista<string>* intereses;
 
        Pila<string>* actividadesRecientes;
 
    public:
 
        Usuario(string nombreusuario){
            nombre =  nombreusuario;
            amigos = new Lista<string>;
            intereses = new Lista<string>;
            actividadesRecientes = new Pila<string>;
        }




1
2
3
4
5
6
7
8
9
10
11
12
void agregarUsuario(string nombre) {
 
             Usuario nuevoUsuario(nombre);
             if (!(this->usuarios->Esta(nuevoUsuario)))
             {
                 this->usuarios->agregar(nuevoUsuario);
 
             }
             else
                this->consola->getSalida()<< "El usuario "<<nombre<<" ya existe."<<endl;
             return;
        }


Luego cuando quiere crear un nodo miren:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void insertar(T dato, Nodo<T>* anterior, Nodo<T>* siguiente) {
            /* crea el nodo */
           Nodo<T>* nuevo = new Nodo<T>(dato);// HERE ITS THE PROBLEM 
               
            nuevo->setAnterior(anterior);
            nuevo->setSiguiente(siguiente);
            /* actualiza los nodos anterior y siguiente */
            if (anterior != NULL) {
                anterior->setSiguiente(nuevo);
            } else {
                /* actualiza el primero en caso de insertar al comienzo */
                this->getLista()->setPrimero(nuevo);
            }
            if (siguiente != NULL) {
                siguiente->setAnterior(nuevo);
            } else {
                /* actualiza el último en caso de insertar al final */
                this->getLista()->setUltimo(nuevo);
            }
            /* se queda en la posición que insertó */
            this->setActual(nuevo);
        }
 


NODO & CONSTRUCTOR:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class T> class Nodo {
    private:
        
        T dato;
        
        Nodo<T>* siguiente;
        
        Nodo<T>* anterior;
 
 
    public:
        
 
       Nodo(T dato) { //This line calls Usuario::Usuario() can't understand why is it call.
            this->setDato(dato);
            this->setSiguiente(NULL);
            this->setAnterior(NULL);
        }
 


Thx very much for ur help.
Last edited on
Hablamos Inglés aquí. Gracias.

-Albatros
sry, i didn't know. I have translated my problem, hope someone can help.
You need a default constructor (one that takes no parameters) for the Usuario class.

(because if you try to make a node using the Usuario class like this:
Nodo<Usaurio> myNode;

then

1
2
3
4
5
6
template<class T> class Nodo {
    private:
        
        T dato; //this line will cause an error because  dato variable needs a default constructor.
        
   
Change the Nodo constructor
1
2
3
4
5
Nodo(T d) : dato(d) { //This line calls a constructor for dato. in this case dato(const dato &) T (const T&)
//         this->setDato(dato);
            this->setSiguiente(NULL);
            this->setAnterior(NULL);
} 
With the initializer list you call the constructors of your members. If you don't use it, the default constructor is invoked

guestgulkan wrote:
(because if you try to make a node using the Usuario class like this:
Nodo<Usaurio> myNode;
The point is that he can't construct a Nodo in that way, because Nodo doesn't have a default constructor.

void agregarUsuario(string nombre); To which class this method belongs?
Last edited on
[quote = ne555]
The point is that he can't construct a Nodo in that way, because Nodo doesn't have adefault constructor.[/quote]

That is what I said - re-read my post.
You say default for Usuario.
I say that you can't do this: Nodo<int> n; because there is no Nodo::Nodo(). (And maybe this is correct)

@kmanus: be careful with the pointers. Maybe you could just avoid them.
I said he can't do it because he has no default constructor, which is what the original message is all about.
This is what I wrote:
You need a default constructor (one that takes no parameters) for the Usuario class.

(because if you try to make a node using the Usuario class like this:
Nodo<Usaurio> myNode;

then


template<class T> class Nodo {
private:

T dato; //this line will cause an error because dato variable needs a default constructor.




FFS
Topic archived. No new replies allowed.