2016-11-14 118 views
1

所以,我不斷收到一個錯誤,說我需要一個不合格的id,並且不知道是什麼給了我錯誤。 請幫忙。錯誤:在'''''令牌'之前預期的不合格id節點()

錯誤發生在類的Node()部分。

error: expected unqualified-id before ‘)’ token 
    Node() 
    ^

下面的代碼:

#include <iostream> 
    #include <string> 

    using namespace std; 

    class AHuffman 
    { 

    public: 
      class Node 
      { 
        Node* right_child; 
        Node* left_child; 
        Node* parent; 
        Node* sibling; 
        int weight; 
        int number; 
        string data; 
      }; 

      Node() 
      { 
        right_child = NULL; 
        left_child = NULL; 
        parent = NULL; 
        sibling = NULL; 
        weight = 0 
        number = 1; 
        data = ""; 
      } 

    //  int encode(string* msg, char** result, int rbuff_size); 
    //  int decode(string* msg, char** result, int rbuff_size); 
      AHuffman(string* alphabet); 
      ~AHuffman(); 
    }; 

    int main(int argc, const char* argv[]) 
    { 
      if(argc != 4){ 
        //Invalid number of arguments 
        cout << "invalid number of arguments" << endl; 
        return 1; 
      } 
      string* alphabet = new string(argv[1]); 
      string* message = new string(argv[2]); 
      string* operation = new string(argv[3]); 


      return 0; 

    } 

回答

4

因爲你把節點的構造函數的類外:

無論如何,你應該member initializer list,而不是在構造函數體中的成員初始化。

class Node 
{ 
    Node* right_child; 
    Node* left_child; 
    Node* parent; 
    Node* sibling; 
    int weight; 
    int number; 
    string data; 
public: 
    Node() 
     : right_child(0), left_child(0), 
     parent(0), sibling(0), 
     weight(0), number(1) 
    { 
    } 
}; 

另要注意,你並不需要在C多new ++

0

貌似不好複製粘貼代碼。 'Node 構造函數首先應該在'Node'類中有一個聲明,或者像下面那樣移動裏面的定義。

您decla

class Node 
     { 
     private:     // private members 
       Node* right_child; 
       Node* left_child; 
       Node* parent; 
       Node* sibling; 
       int weight; 
       int number; 
       string data; 
     public:     // make it public so the class can actually be constructible 
       Node() 
       { 
         right_child = NULL; 
         left_child = NULL; 
         parent = NULL; 
         sibling = NULL; 
         weight = 0;   // and delimiter here 
         number = 1; 
         data = ""; 
       } 
     }; 
+0

好,謝謝你指出了那些我不知道的構造函數是在聲明。 – helpmee

相關問題