このエラーを表示するために必要なコードを最小限に抑えることにしました。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 プロジェクトに入力すると、エラーや警告なしでそのままコンパイルされます。ただし、別の cpp ファイルをインクルードし、次のようにリスト ヘッダーをインクルードしようとします。
// anyNamedFile.cpp file
#include "hc_list.h"
プロジェクトに cpp ファイルを含めると、次のようなコンパイラ エラーが発生します。
error: expected initializer before '&' token
私は自分が間違っていることを理解していません。実際に助けを借りることができます。