1

Llevo varios días con el mismo error, "referencia a 'Container::nextId' sin definir".

El .h

class Container

friend std::ostream& operator<< (std::ostream &os, const Container& obj);


protected:
    unsigned int id;
    unsigned int value;
    unsigned int weight;
    static unsigned int nextId;
    static const unsigned int kMINWEIGHT = 100;
    static const unsigned int kMINVALUE = 100;

public:
    Container(unsigned int weight, unsigned int value);
    void static resetNextId();
    unsigned int getId() const;
    unsigned int getValue() const;
    unsigned int getWeight() const;
    void setWeight(unsigned int weight);
    void setValue(unsigned int value);

Y la declaración del método donde da error, en el .cc:

void Container::resetNextId(){

    Container::nextId = 1;

}

He buscado mucha información, y parece ser que el error proviene de modificar el atributo nextId, que es static. He probado todo tipo de cosas, pero no logro averiguar como solucionarlo. El error se produce en la línea donde aparece 'Container::nextId = 1;'

Gracias de antemano, y un saludo.

2 respuestas 2

1

Los datos estáticos de clase deben ser definidos fuera de la clase:

class Container {

friend std::ostream& operator<< (std::ostream &os, const Container& obj);


protected:
    unsigned int id;
    unsigned int value;
    unsigned int weight;
    static unsigned int nextId;
    static const unsigned int kMINWEIGHT = 100;
    static const unsigned int kMINVALUE = 100;

public:
    Container(unsigned int weight, unsigned int value);
    void static resetNextId();
    unsigned int getId() const;
    unsigned int getValue() const;
    unsigned int getWeight() const;
    void setWeight(unsigned int weight);
    void setValue(unsigned int value);
};

unsigned int Container::nextId = 1;
unsigned int Container::kMINWEIGHT = 100;
unsigned int Container::kMINVALUE = 100;
0

El código que proporcionas no debería fallar. He probado a compilarlo en mi equipo y compila bien. Lo único que falta son los corchetes de la clase en el .h que quedaría así

class Container {

friend std::ostream& operator<< (std::ostream &os, const Container& obj);


protected:
    unsigned int id;
    unsigned int value;
    unsigned int weight;
    static unsigned int nextId;
    static const unsigned int kMINWEIGHT = 100;
    static const unsigned int kMINVALUE = 100;

public:
    Container(unsigned int weight, unsigned int value);
    void static resetNextId();
    unsigned int getId() const;
    unsigned int getValue() const;
    unsigned int getWeight() const;
    void setWeight(unsigned int weight);
    void setValue(unsigned int value);
};
2
  • No tienes idea de porque puede aparecer el error? Más concretamente es este: g++ -Wall -g Base.o Ship.o Container.o Util.o prac3.o -o prac3 Container.o: En la función Container::resetNextId()': /home/luis/Escritorio/Cpp/Practica 3/autocorrector-prac3/src/Container.cc:90: referencia a Container::nextId' sin definir collect2: error: ld returned 1 exit status makefile:6: recipe for target 'prac3' failed make: *** [prac3] Error 1
    – Baidal_17
    el 1 may. 2019 a las 15:47
  • De echo, en cualquier parte del Container.cc donde hago referencia a 'nextId' me aparece el mismo error, como por ejemplo: id = nextId o nextId++
    – Baidal_17
    el 1 may. 2019 a las 16:21

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.