gpt4 book ai didi

c++ - 重载流插入运算符错误,无法编译

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:15 25 4
gpt4 key购买 nike

我正在尝试重载 operator<<对于我的 Graph类,但我不断收到各种错误:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '<'

我有 operator<< 的原型(prototype)放在 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<<的非成员(member)声明

关于c++ - 重载流插入运算符错误,无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com