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. 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 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. However, then I include another cpp file and attempt to include the list header, like the following:
// anyNamedFile.cpp file
#include "hc_list.h"
When I include any cpp file into the project, I get a compiler error:
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.Try adding
in your header.