2011-11-01 19 views
2

我一直在做這個項目,但每次我試圖建立,我得到這些錯誤消息:不確定的typedef安置和類

 
|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 ===| 

林不知道這些錯誤信息的原因是因爲我有我的口香糖球節點類內聲明的類和

在我的Stack類#include "Node.h""#include "Stack.h"在我的主。任何建議,將不勝感激。

我Node.h文件看起來像這樣:

#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 
+0

您不需要'Stack.h'中的'typedef',因爲它包含在'Node.h'中。 –

+3

不要在頭文件中使用'namespace'。 –

+0

沒有使用名稱空間,它不能識別字符串作爲類型 – 123me

回答

2

Where is the Gumball::Gumball()構造函數中定義?這是在gumball.cpp文件中嗎?如果沒有,然後嘗試內聯定義它:

class Gumball { 
    public: 
     Gumball() {} 
+0

構造函數在.cpp文件中定義 – 123me

+0

好的,你的鏈接步驟包括構建中的.cpp文件嗎?基本上,你顯示的錯誤是一個鏈接錯誤,說鏈接器不知道在哪裏找到'Gumball :: Gumball()'的定義。 –

+0

我試圖用內聯的方法來定義構造函數:class Gumball { public: Gumball(){color =「」,counter = 0}; 串色; int counter; };但我收到一條錯誤消息,說「錯誤:預期」;「在'}'令牌之前 – 123me

1

您是否實現了任何類方法?我看到的只是一堆類的聲明。

+0

是的,它們都是在.cpp文件中實現的。 – 123me