【问题标题】:unqualified id before a friend function [closed]朋友功能之前的不合格ID [关闭]
【发布时间】:2018-11-29 03:47:43
【问题描述】:

我收到以下错误:

..\src\test.cpp:17:20: error: expected unqualified-id before 'try'
         friend int try(Complex);

请帮助并告诉我为什么这段代码会产生错误:

#include <iostream>
using namespace std;
class Complex
{
private:
    int a, b;
public:
    showData()
    {   
        cout<<"\na= "<<a<<" b= "<<b;
    }
    Complex(int x, int y)
    { //constructer
        a = x;
        b = y;
    }
    friend int try(Complex);
    //friend function
};
int try(Complex c)
{   
    cout<<"You areworking in try now";
    cout<<"\noutput from friend fun : "<<c.a<<" "<<c.b;
}
int main()
{
    Complex c1(3, 4);
    try(c1);
    return 0;
}

【问题讨论】:

  • 请使用tour,访问help center,阅读How to Ask以及如何创建minimal reproducible example。最重要的是,代码和错误消息都是文本,因此请照此发布。没有图片,也没有任何图片链接。复制和粘贴内容比抓取屏幕截图并上传要省力。它还使您的问题可搜索
  • 我强烈建议不要发布图片。它们很难搜索,也更难编译。除非图片真的值一千字,或者问题是关于像素的,否则不要使用它们。
  • 我建议您也阅读idownvotedbecau.se 中的原因列表。其中一些与您的帖子有关,可以帮助您在未来提出更好的问题。

标签: c++ compiler-errors friend


【解决方案1】:

首先让我们将问题简化为重复问题的Minimal Complete and Verifiable example

int try;

不需要太多代码,因为try is a reserved word。如果没有编译器expecting a try/catch` block,则不能在程序中使用try

解决方案:不要使用try 作为标识符。请改用try_func 或描述正在尝试的内容。

附加说明:showData() 需要返回类型。很可能应该是void showData()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多