I'm trying to overload operator<<
for my Graph
class but I keep getting various errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '<'
I have the prototype for operator<<
placed right above the Graph
class definition. operator<<
's definition is at the very bottom of the file. Do the errors have something to do with header guards?
Here's Graph.h
:
#ifndef GRAPH
#define GRAPH
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include "GraphException.h"
#include "Edge.h"
using namespace std;
template <class VertexType>
ostream& operator<<( ostream& out, const Graph<VertexType>& graph );
/** An adjacency list representation of an undirected,
* weighted graph. */
template <class VertexType>
class Graph
{
friend ostream& operator<<( ostream& out, const Graph& graph );
// stuff
}
template <class VertexType>
ostream& operator<<( ostream& out, const Graph<VertexType>& graph )
{
return out;
}
#endif GRAPH
and here's main
:
#include <iostream>
#include "Graph.h"
using namespace std;
const unsigned MAX_NUM_VERTICES = 9;
int main()
{
// create int graph:
Graph<int> iGraph( MAX_NUM_VERTICES );
// add vertices and edges
cout << iGraph;
return 0;
}
The declaration of operator<<
is missing a declaration for Graph
. One solution is to declare the class before the operator<<
declaration:
template <class VertexType>
class Graph;
Or you can omit the declaration of operator<<
outside the class entirely, as a friend
declaration constitutes a nonmember declaration of operator<<
as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.