使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
Can't seem to include any cpp file other than main.cpp when using template class
我决定将必要的代码减少到显示此错误所需的最低限度。我有一个存在于 hc_list.h 文件中的 STL 列表包装器模板类。整个代码如下:
// hc_list.h file
#ifndef HC_LIST_H
#define HC_LIST_H
#include <cstdlib>
#include <list>
template <typename T>
class hcList
{
private:
std::list<T> selfList ; // a single internal STL list to hold the values
public:
hcList(void) {} ;
~hcList(void){} ;
// The error occurs on the line below
template <typename U> friend std::ostream& operator<<(std::ostream &, const hcList<U> &) ;
} ;
#endif // HC_LIST_H
此代码包含在 main.cpp 文件中,其中 main 函数如下:
// main.cpp file
#include <iostream>
#include "hc_list.h"
int main()
{
std::cout << "Begin Test" << std::endl;
return 0;
}
此代码在输入到 CodeBlocks 项目中时将按原样编译,错误或警告为 0。但是,然后我包含另一个 cpp 文件并尝试包含列表标题,如下所示:
// anyNamedFile.cpp file
#include "hc_list.h"
当我将任何 cpp 文件包含在项目中时,我收到编译器错误:
error: expected initializer before '&' token
我不明白我做错了什么,真的可以使用一些帮助。
你的头文件使用 std::ostream
,(就在&
之前),但不包括任何可能声明它的头。
尝试添加
#include <iosfwd>
在您的标题中。
相关文章:
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 在 main.cpp 的上下文中找不到目录(带有 CMake 的快板)
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 如何在 cpp 的 main() 中调用此函数
- 访问除main.cpp之外的.cpp文件中的QMLEngine/rootObject属性
- 生成main.c和main.cpp的单个Makefile
- C 函数未从 main.cpp C++ 调用
- 代码块始终在项目中运行 main.cpp
- 运行单独的 cpp 文件,每个文件都有 main()
- main.cpp 如何知道包含标头的源文件?
- 如何使用 c++ 从另一个 cpp 文件中的 main.cpp 调用实例化类
- 当我有多个类时,我应该以线性样式包含它们还是只包含在 main.cpp 中?
- 无法引用.cpp中的函数,以及 main 中的 .h 文件。C++
- main.cpp从 .h 文件调用公共虚拟方法
- main.cpp:(.text+0x8ea):在C++中未定义的引用
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 实用程序函数不会在 .h 文件中初始化并在 main 中定义.cpp
- 编译php-cpp main.cpp文件时出错
- 多个文件的类用法 .h .cpp main.cpp