gpt4 book ai didi

c++ - 连接两个 C++ 文件(List.cc 和 Queue.cc)时出现问题

转载 作者:行者123 更新时间:2023-11-28 00:34:37 28 4
gpt4 key购买 nike

我必须使用我之前创建的列表文件在 C++ 中编写一个队列,并且我很难编译所有内容。

我目前遇到的问题是,当我编译时出现错误: Queue.h:7:2: 错误:'List' 没有命名类型

如何正确连接队列文件和列表文件?

以下是我使用的文件: 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();
};

列表.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;
}
}

队列.h

#ifndef LIST_H
#define LIST_H

class Queue {
public:

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

Queue();
~Queue();
};

#endif

队列.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(){
}

感谢您的帮助!

最佳答案

编译器告诉你哪里出了问题:

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 仅在编译器必须知道类/结构的大小时才是必需的。

关于c++ - 连接两个 C++ 文件(List.cc 和 Queue.cc)时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460109/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com