错误:未在此范围内声明“朋友成员函数名称" [英] Error: 'Friend Member Function Name' was not declared in this scope

查看:261
本文介绍了错误:未在此范围内声明“朋友成员函数名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将所有C ++ Windows应用程序迁移到Ubuntu Linux.此应用程序可以在Windows 7 OS上的Visual Studio 2015社区上正常运行.但是,在Ubuntu Linux上的代码块中运行时,它会给出错误.我已经使用以下简单的Person类复制了错误消息.

I am in the process of moving all of my C++ Windows applications to Ubuntu Linux. This application runs fine on Visual Studio 2015 Community on Windows 7 OS. However, it gives an error when running in Code Blocks on Ubuntu Linux. I have replicated the error message I am getting using the following simple Person class.

错误消息:未在此范围内声明"comparePersonAge"

Error Message: 'comparePersonAge' was not declared in this scope

Person.h

#ifndef Person_h
#define Person_h

#include <string>

class Person
{
private:
    int age;
    std::string name;
public:
    Person(int a, std::string n) : age(a), name(n) {}

    int getAge()
    {
        return age;
    }
    std::string getName()
    {
        return name;
    }
    inline friend bool comparePersonAge(const Person& p1, const Person& p2)
    {
        return p1.age < p2.age;
    }
};

#endif

main.cpp

#include <iostream>
#include <algorithm>
#include <vector>
#include "Person.h"

int main()
{
    Person p1(93, "harold");
    Person p2(32, "james");
    Person p3(67, "tracey");

    std::vector<Person> v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);

    std::sort(v.begin(), v.end(), comparePersonAge);

    std::cout << v[0].getAge() << " " << v[1].getAge() << " " << v[2].getAge()  << std::endl;
}

在Windows计算机上,输出为:32 67 93符合预期.在Linux上,错误消息如上所述.

On Windows machine, the output is: 32 67 93 as expected. On Linux, the error message is as written above.

注意:另一个人DJR在这篇文章中讨论了此问题:

Note: Someone else name DJR discusses this issue in this post: Friend function not declared in this scope error. However, his explanation is very vague I and don't follow his steps.

他写道:

以前的注释应该读为:这是Linux方面的错误.该代码应按书面形式工作.我现在有在Windows端可以正常编译的代码,当我将其移至Linux端时,也会遇到相同的错误.显然,您在Linux端使用的编译器看不到/使用头文件中的friend声明,因此会出现此错误.通过在使用该函数之前简单地在C ++文件中移动该朋友函数的定义/实现(例如:可能在函数回调分配中使用),这解决了我的问题,也应该解决您的问题.

Previous comment should have read: It is a bug on the the Linux side. The code should work as written. I have code right now that compiles fine on the Windows side and when I move it to the Linux side I get the same error. Apparently the compiler that you are using on the Linux side does not see/use the friend declaration in the header file and hence gives this error. By simply moving the friend function's definition/implementation in the C++ file BEFORE that function's usage (e.g.: as might be used in function callback assignment), this resolved my issue and should resolve yours also.

通过使用C ++文件中的朋友功能定义之前,我不知道他是什么意思.这到底是什么意思?

I don't know what he means by by moving the friend function's definition in the C++ file before the function's usage. What does this mean precisely?

推荐答案

标准7.3.1.2/3:

Standard 7.3.1.2/3 :

首先在名称空间中声明的每个名称都是该名称的成员 命名空间. 如果非本地类中的朋友声明首先声明 类或函数的朋友类或函数是 最内层的封闭名称空间.找不到朋友的名字 不合格的查询(3.4.1)或合格的查询(3.4.3)直到出现 该名称空间范围中提供了匹配的声明( 在授予友谊的班级定义之前或之后).如果是朋友 函数被调用,其名称可以通过名称查找找到 考虑来自名称空间和与 函数参数的类型(3.4.2).如果在朋友的名字 声明既不合格也不是模板ID,声明 是一个函数还是一个详尽的类型说明符,用于确定的查找 该实体先前是否已被宣布不得考虑任何 作用域位于最内层的封闭命名空间之外.

Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2) . If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.

好了,在与@Niall进行少量讨论之后,我意识到MSVC ++在这种情况下是错误的,因为ADL仅出现在函数调用表达式中,并且由于std::sort仅传递了函数名,即comparePersonAge,没有函数comparePersonAge应该在调用std::sort时找到.因此,我认为GCC和Clang是正确的

Ok after little discussion with @Niall I realized that MSVC++ is wrong in this case, since ADL only happens in function call expression and since std::sort is being passed just name of function i.e comparePersonAge, no function comparePersonAge should be found at the time of call to std::sort . Hence GCC and Clang are correct I think

这篇关于错误:未在此范围内声明“朋友成员函数名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆