我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 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
我不明白我做错了什么,真的需要一些帮助。
您的头文件使用 std::ostream
,(就在 &
之前)但不包含任何可能声明它的头文件。
thử thêm
#include
在你的标题中。
Tôi là một lập trình viên xuất sắc, rất giỏi!