簡體   English   中英

重載的流插入運算符錯誤,將無法編譯

[英]overloaded stream insertion operator errors, won't compile

我正在嘗試為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;
}

operator<<的聲明缺少Graph的聲明。 一種解決方案是在operator<<聲明之前聲明該類:

template <class VertexType>
class Graph;

或者,您也可以在類之外完全省略operator<<的聲明,因為friend聲明也構成operator<<的非成員聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM