![](/img/trans.png)
[英]Main.cpp can't access variables and functions in header file and other .cpp file
[英]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.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>
在您的标题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.