2014-01-30 104 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