[英]Can't seem to include any cpp file other than main.cpp when using template class
I decided to cut the necessary code down to the bare minimum needed to display this error. 我决定将必要的代码减少到显示此错误所需的最低限度。 I have an STL list wrapper template class that exists in hc_list.h file.
我在hc_list.h文件中存在一个STL列表包装器模板类。 The entire code is below:
整个代码如下:
// 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
This code is included in the main.cpp file, where the main function is below: 该代码包含在main.cpp文件中,其主要功能如下:
// main.cpp file
#include <iostream>
#include "hc_list.h"
int main()
{
std::cout << "Begin Test" << std::endl;
return 0;
}
This code, when entered into a CodeBlocks project will compile as is with 0 errors or warnings. 将此代码输入CodeBlocks项目后,将按0正确显示错误或警告。 However, then I include another cpp file and attempt to include the list header, like the following:
但是,然后我包含另一个cpp文件,并尝试包含列表标头,如下所示:
// anyNamedFile.cpp file
#include "hc_list.h"
When I include any cpp file into the project, I get a compiler error: 当我在项目中包含任何cpp文件时,都会出现编译器错误:
error: expected initializer before '&' token
I do not understand what I am doing wrong, and could really use some help. 我不明白自己在做什么错,真的可以使用一些帮助。
Your header file uses std::ostream
, (just before an &
) but does not include any header which might declare it. 您的头文件使用
std::ostream
(在&
之前),但不包括任何可能声明它的头。
Try adding 尝试添加
#include <iosfwd>
in your header. 在您的标题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.