【问题标题】:C++ error unexplained: error: expected initializer before '&' tokenC++ 错误无法解释:错误:“&”标记之前的预期初始化程序
【发布时间】:2011-04-12 13:09:38
【问题描述】:

我知道还有其他一些类似的帖子,但我已经在这个单一错误上运行了一个多小时,无法弄清楚。这是造成麻烦的代码

istream& operator>>(istream& in, UndirectedGraph& g)
{
    int numVerticies;
    in >> numVerticies;
    g = UndirectedGraph(numVerticies);

    for(int i = 0; i < numVerticies; i++)
    {
        int temp;
        in >> temp;
        if(temp != i)
        {
            g.linkedAdjacencyList[i]->value = temp;
        }
    }

    int edges;
    in >> edges;
    g.edges = edges;
    for(int i = 0; i < edges; i++)
    {
        int first;
        int second;
        in >> first >> second;
        addEdge(first, second);
    }
    return in;
}

ostream& operator<<(ostream& out, UndirectedGraph& g)
{
    out << g.numVerticies << endl;

    for(int i = 0; i < g.numVerticies; i++)
    {
        out << g.linkedAdjacencyList[i] << " ";
    }
    out << endl;

    out << g.edges << endl;

    for(int i = 0; i < g.numVerticies; i++)
    {
        out << linkedAdjacencyList[i]->value;
        Node* whereto;
        whereto = linkedAdjacencyList[i]->adj;
        while(whereto->adj != NULL)
        {
            out << " " << whereto->value;
            whereto->adj = whereto->adj->adj;
        }
    }
    return out;
}

int main()
{

    ifstream inFile;
    inFile.open("hw8.in");

    UndirectedGraph graph;
    inFile >> graph;

...

这里,错误在第 1 行和第 28 行,istream 和 ostream 重载。

感谢您的帮助!

【问题讨论】:

  • 第 1 行和第 28 行是什么?
  • @Daniel istream 和 ostream 声明

标签: c++ istream ostream


【解决方案1】:

这个:

void UndirectedGraph::istream& operator>>(istream& in, UndirectedGraph g)

没有意义!你可能想要:

istream& operator>>(istream& in, UndirectedGraph g)

话虽如此,您的函数定义中似乎没有返回任何内容。

【讨论】:

  • 确实如此,我完全忽略了这一点。按照建议,我将行(istream 和 ostream)更改为 UndirectedGraph::istream&amp; operator&gt;&gt;(istream&amp; in, UndirectedGraph&amp; g),但现在我在“&”标记错误之前得到了预期的构造函数、析构函数或类型转换
  • @Jacob:如果对您问题的编辑具有代表性,那么您没有对代码进行正确的更改。再看看!
  • 如果重要的话,UndirectedGraph 存在于我包含的 .h 文件中,并且我正在从 .cpp 文件中重载
  • @Oli 哇!哈哈,当我看到发布的 cmets 时我正在编辑,所以我保持不变。现在应该好了!
  • @Oli,你是对的,我直接忘了返回任何东西。但它仍然无法通过声明中的expected constructor, destructor, or type conversion before ‘&amp;’ token
【解决方案2】:
void UndirectedGraph::istream& operator>>(istream& in, UndirectedGraph g)

这里有两种返回类型:voidUndirectedGraph::istream&amp;。一个人需要去。其他方法也一样。

此外,我假设您的意思是 std::istream,而不是 UndirectedGraph::istream,对吧?

最后,要使这段代码正常工作,您需要通过引用传递参数g,否则您将无法更改它。

这给我们留下了:

std::istream& operator>>(std::istream& in, UndirectedGraph& g)

【讨论】:

  • 也告诉查尔斯沃思,我在 istream 和 ostream& 中取出了 ostream 中的空白。现在我得到另一个错误? “&”标记之前的预期构造函数、析构函数或类型转换
  • @Konrad:@Wakefield 和@Oli 是对的,在 istream 或 ostream 之前没有 std::UndirectedGraph::。请参阅 Oli 帖子上的 cmets,当我尝试使用输入运算符时收到一个新错误
  • @JacobL 但是我的答案,与他们的不同,会奏效——试试吧! Don 的回答不起作用,因为 operator &gt;&gt; 不能成为 UndirectedGraph 的有意义的成员。代码甚至可以编译,但您将无法正确使用它。您确实需要使用我的答案中的代码。
  • @Konrad 你是对的,两者都有效。但我仍然有新的错误:(.text+0x299): undefined reference to 'UndirectedGraph::UndirectedGraph()' 我无法摆脱
  • @JacobL 这只是意味着您的类缺少无参数构造函数。
【解决方案3】:

似乎有几个问题。首先,插入和提取运算符是成员方法还是只是辅助函数?对于后者,签名类似于

istream & operator>>(istream& in, UndirectedGraph &g)

对于前者

istream &UndirectedGraph::operator>>(istream& in, UndirectedGraph &g)

无论哪种情况,如上图所示,您都应该将 UndirectedGraph 参数设为引用参数。否则,您只是按值传递参数,这可能不是您想要的。

【讨论】:

    【解决方案4】:

    有点重申@don-wakefield 所说的话。我认为您希望您的运营商分别返回 std::istream& 和 std::ostream& (而不是 UndirectedGraph::istream 和 UndirectedGraph::ostream)。

    所以签名看起来像:

    std::istream& UndirectedGraph::operator>>(istream& in, UndirectedGraph g)
    std::ostream& UndirectedGraph::operator<<(ostream& out, UndirectedGraph& g)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多