【发布时间】:2013-05-05 06:24:52
【问题描述】:
我正在尝试为我的Graph 类重载operator<<,但我不断收到各种错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '<'
我将operator<< 的原型放在Graph 类定义的正上方。 operator<< 的定义位于文件的最底部。这些错误是否与标头保护有关?
这里是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
这里是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;
}
【问题讨论】:
-
如果您尝试创建SSCCE,将来人们会更有帮助。以这种方式思考的一个很好的副作用是,您可能会在发布问题之前弄清楚您的问题。
-
谢谢,我不知道 SSCCE。我删掉了所有我觉得无关紧要的东西。问题是,我通常不是无关紧要和无关紧要的最佳仲裁者。
-
这是一个整数图。编辑它。