2014-01-30 40 views
0

我必須用C++編寫一個隊列,使用我之前創建的List文件,而且我要花費大量時間來編譯。連接兩個C++文件(List.cc和Queue.cc)時出現的問題

我目前遇到的問題是,我編譯時出現錯誤: Queue.h:7:2:錯誤:「名單」沒有指定類型

如何去正確連接我的隊列文件和我的列表文件?

下面是我使用的文件: List.h

//an item in the list 
    struct ListNode { 
    int _value; 
    ListNode * _next; 
}; 

class List { 
public: 
    //Head of list 
    ListNode * _head; 

    int remove_front(); 
    void insertSorted(int val); 
    void append (int val); 
    void prepend (int val); 
    int lookup(int _value); 
    int remove(int val); 
    void print(); 
    List(); 
    ~List(); 
}; 

List.cc

// 
// Implement the List class 
// 

#include <stdio.h> 
#include "List.h" 

ListNode * _head = new ListNode(); 

//remove the first node in the list 
int 
List::remove_front(){ 
    int ret; 
    if(_head == 0){ 
     return -1; 
    } 
    ret = _head->_value; 
    ListNode *temp = new ListNode(); 
    temp = _head->_next; 
    delete(_head); 
    _head = temp; 
    return ret; 
} 

// 
// Inserts a new element with value "val" in 
// ascending order. 
// 
void 
List::insertSorted(int val){ 

    ListNode* new_node = new ListNode(); 
    new_node->_value = val; 
    ListNode* current = new ListNode(); 

    if(_head == 0){ 
     _head = new_node; 
    }else{ 
     current = _head; 
     ListNode* prev = 0; 

     while(current != 0){ 
      if(new_node->_value > current->_value){ 
       prev = current; 
       current = current->_next; 
      }else{ 
       break; 
      } 
     } 
     if(current == _head){ 
      new_node->_next = _head;  
      _head = new_node; 
     }else{ 
      new_node->_next = current; 
      prev->_next = new_node; 
     } 

    } 
} 

// 
// Inserts a new element with value "val" at 
// the end of the list. 
// 
void 
List::append(int val){ 

    //create a new node to hold the given value 
    ListNode *new_node = new ListNode();   
    new_node->_value = val; 

    //if the list is empty 
    if(_head == 0){ 
     //set the new node to be the head 
     _head = new_node; 
     return ; 
    } 

    //create a node pointer to the current position (starting at the head) 
    ListNode *current = new ListNode(); 
    current = _head; 

    //Loop through the list until we find the end 
    while(current->_next != NULL){ 
     current = current->_next; 
    } 

    current->_next = new_node; 
} 

// 
// Inserts a new element with value "val" at 
// the beginning of the list. 
// 
void 
List::prepend(int val){ 
    ListNode *new_node = new ListNode; 
    new_node->_value = val; 

    if(_head == 0){ 
     _head = new_node; 
     return ; 
    } 

    ListNode *temp = new ListNode; 
    temp = _head; 


    _head = new_node; 
    _head->_next = temp; 
} 

// Removes an element with value "val" from List 
// Returns 0 if succeeds or -1 if it fails 
int 
List:: remove(int val){ 

    if(_head == 0){ 
     printf("List is already empty.\n"); 
     return -1; 
    } 

    ListNode *current = new ListNode(); 
    ListNode* prev = new ListNode(); 
    current = _head; 

    while(current != 0){ 
     if(current->_value == val){ 
      if(current == _head){ 
       _head = _head->_next; 
       delete(current); 
       return 0; 
      }else{ 
       prev->_next = current->_next; 
       delete(current); 
       return 0; 
      } 
     }else{ 
      prev = current; 
      current = current->_next; 
     } 
    } 
    return -1; 
} 

// Prints The elements in the list. 
void 
List::print(){ 
    ListNode* current = new ListNode(); 
    while(current != 0){ 
     printf("%d\n", current->_value); 
     current = current->_next; 
    } 
} 

// 
// Returns 0 if "value" is in the list or -1 otherwise. 
// 
int 
List::lookup(int val){ 
    ListNode * current = new ListNode(); 
    current = _head; 
    while(current != NULL){ 
     if(current->_value == val){ 
      return 0; 
     } 
     else{ 
      current = current->_next; 
     } 
    } 
    return -1; 
} 

// 
// List constructor 
// 
List::List(){ 

} 

// 
// List destructor: delete all list elements, if any. 
// 
List::~List(){ 
    ListNode* current = _head; 
    while(current != NULL){ 
     ListNode* next = current->_next; 
     delete current; 
     current = next; 
    } 
} 

Queue.h

#ifndef LIST_H 
#define LIST_H 

class Queue { 
public: 

    List* queue_list; 
    void enqueue(int val); 
    int dequeue(); 

    Queue(); 
    ~Queue(); 
}; 

#endif 

Queue.cc

#include <stdio.h> 
#include "List.h" 
#include "Queue.h" 

List *queue_list = new List(); 

void 
Queue::enqueue(int val){ 
    this->queue_list->prepend(val); 
} 

int 
Queue::dequeue(){ 
    int value = this->queue_list->remove_front(); 
    return value; 
} 

Queue::Queue(){ 
    //do nothing 
} 

Queue::~Queue(){ 
} 

queue_main.cc

#include <stdio.h> 

#include "Queue.h" 
#include "List.h" 

Queue *queue; 

int main(){ 
} 

感謝您的幫助!

回答

3

編譯器會告訴你什麼是錯的:

Queue.h:7:2: error: 'List' does not name a type

在閱讀Queue.h,編譯器不可能知道什麼List是,因爲沒有什麼在這個文件中定義它。

您只需添加一個向前聲明

#ifndef LIST_H 
#define LIST_H 

class List; // this is a forward declaration. 

class Queue { 
public: 

    List* queue_list; 
    void enqueue(int val); 
    int dequeue(); 

    Queue(); 
    ~Queue(); 
}; 

#endif 

或者(但不是必要在這裏),你可以簡單地#include List.h。經驗法則是:如果可能,請使用前向聲明。如果編譯器抱怨,請將其替換爲相應的include。只有編譯器必須知道類/結構的大小時,才需要include

+0

這不是一個準確的規則。如果編譯器需要知道相關類型的大小,你只需要包含'List.h'。如果它是一個成員變量。如果您將它用作參數,假設沒有隱式轉換,則僅使用前向聲明即可保證安全。 –

+0

這很有道理。感謝您的幫助! – user3253680

+0

@MarkIngram你是對的,規則調整。 ;-) – stefan