cuốn sách gpt4 ai đã làm

Dường như không thể bao gồm bất kỳ tệp cpp nào ngoài main.cpp khi sử dụng các lớp mẫu

In lại Tác giả: Walker 123 更新时间:2023-11-28 08:04:34 26 4
mua khóa gpt4 Nike

我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 hc_list.h 文件中的 STL 列表包装器模板类。完整代码如下:

// hc_list.h file
#ifndef HC_LIST_H
#define HC_LIST_H

#include
#include

mẫu
class hcList
{
riêng tư:
std::list selfList ; // a single internal STL list to hold the values

công cộng:
hcList(void) {} ;
~hcList(void){} ;
// The error occurs on the line below
template friend std::ostream& operator<<(std::ostream &, const hcList &) ;
} ;
#endif // HC_LIST_H

这段代码包含在main.cpp文件中,其中main函数如下:

// main.cpp file
#include
#include "hc_list.h"
int chính()
{
std::cout << "Begin Test" << std::endl;
trả về 0;
}

此代码在进入 CodeBlocks 项目时将按原样编译,没有 0 个错误或警告。但是,然后我包含另一个 cpp 文件并尝试包含列表标题,如下所示:

// anyNamedFile.cpp file
#include "hc_list.h"

当我将任何 cpp 文件包含到项目中时,出现编译错误:

error: expected initializer before '&' token

我不明白我做错了什么,真的需要一些帮助。

câu trả lời hay nhất

您的头文件使用 std::ostream,(就在 & 之前)但不包含任何可能声明它的头文件。

thử thêm

#include 

在你的标题中。

关于c++ - 使用模板类时似乎无法包含除 main.cpp 以外的任何 cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564627/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress