我一直在研究此程序,但是每次尝试构建时,都会收到以下错误消息:

|41|undefined reference to `Gumball::Gumball()'
obj\Debug\main.o||In function `main':|
|24|undefined reference to `Gumball::Gumball()'
|24|undefined reference to `Gumball::Gumball()'
obj\Debug\main.o||In function `Z10eatgumballR5StackR7Gumball'
|102|undefined reference to `Gumball::Gumball()'
obj\Debug\Node.o||In function `Node':
|4|undefined reference to `Gumball::Gumball()'
|4|more undefined references to `Gumball::Gumball()' follow
||=== Build finished: 6 errors, 0 warnings ===|

Im not sure what the cause of these error messages are since i have the class Gumball declared inside of my Node class and

#include "Node.h" in my Stack class and"#include "Stack.h" in my main. Any suggestions would be appreciated.

my Node.h file looks like this:

#ifndef NODE_H
#define NODE_H

#include <iostream>
using namespace std;

class Gumball {
    public:
        Gumball();
        string color;
        int counter;
};

typedef Gumball Type;

// Interface file  - Node class definition
class Node {
    public:
        Node();
        Type x;
        Node *n;
        Type getinfo();
        Node *getnext();
        void setinfo(Type x);
        void setnext (Node *n);
    private:
        Type info;
        Node *next;
};

#endif // NODE_H

我的Stack.h文件如下所示:
#ifndef STACK_H
#define STACK_H

#include "Node.h"

typedef Gumball Type;

// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        ~Stack();
        void push(Type);
        Type pop();
        bool isempty();
        //Type size();
        void print();
    private:
        Node *top;
};

#endif // STACK_H

最佳答案

Gumball::Gumball()构造函数在哪里定义?那是在gumball.cpp文件中吗?如果没有,请尝试内联定义:

class Gumball {
    public:
        Gumball() {}

09-04 17:08