1

我试图operator<<为我的Graph班级超载,但我不断收到各种错误:

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;
}
4

1 回答 1

0

的声明operator<<缺少 的声明Graph。一种解决方案是在声明之前operator<<声明类:

template <class VertexType>
class Graph;

或者您可以完全省略operator<<类外部的声明,因为friend声明也构成非成员声明operator<<

于 2013-05-05T06:27:51.507 回答