连接两个C 文件(list.cc and queue.cc)困难
Trouble Connecting Two C++ Files (List.cc and Queue.cc)
我必须使用我之前创建的列表文件在C 中编写一个队列,并且我遇到了艰难的时间来编译所有内容。
我目前遇到的问题是,当我编译时,我会收到错误: 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("%dn", 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(){
}
感谢您的帮助!
编译器告诉您什么问题:
Queue.h:7:2:错误:'列表'不命名类型
在阅读Queue.h
时,编译器可能不知道List
是什么,因为该文件中没有任何定义它的内容。
您只需要添加 forward声明:
#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
才有必要。
相关文章:
- 如何使用CMake编译.proto文件来生成.grpcp.pb.cc和.grpc.pb.h文件
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 位阵列上的快速AND运算
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- BoostPython and CMake
- OpenSSL BIO and SSL_read
- 使用 sprintf 将十六进制0xAABBCC转换为字符串"AA:BB:CC"
- Gurobi GRBModel and GRBmodel in C++
- std::visit and std::variant usage
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- Directx12 and keystrokes
- different between int **arr =new int [ n]; and int a[i][j]?
- C++ getenv and setenv
- Inference pytorch C++ with alexnet and cv::imread image
- Visual Studio 2019 C++ and std::filesystem
- 连接两个C 文件(list.cc and queue.cc)困难