[英]overloaded stream insertion operator errors, won't compile
I'm trying to overload operator<<
for my Graph
class but I keep getting various errors: 我正在尝试为Graph
类重载operator<<
但我不断收到各种错误:
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<<
的原型放在Graph
类定义的上方。 operator<<
's definition is at the very bottom of the file. operator<<
的定义在文件的最底部。 Do the errors have something to do with header guards? 这些错误与标题保护有关吗?
Here's Graph.h
: 这是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
: 这是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
. operator<<
的声明缺少Graph
的声明。 One solution is to declare the class before the operator<<
declaration: 一种解决方案是在operator<<
声明之前声明该类:
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. 或者,您也可以在类之外完全省略operator<<
的声明,因为friend
声明也构成operator<<
的非成员声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.