C++ “非标准语法;使用‘&’创建指向成员的指针”错误 vs2015

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38477997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:46:26  来源:igfitidea点击:

"non-standard syntax; use '&' to create a pointer to member" error vs2015

c++

提问by Tomer

I have this function:

我有这个功能:

std::string Room::getUsersAsString(std::vector<User*> usersList, User * excludeUser)
{
    std::string usersNames = " ";

    for (int i = 0; i < usersList.size(); i++) {
        if (usersList[i]->getUsername() != excludeUser->getUsername) {
            usersNames.append(usersList[i]->getUsername);
            usersNames.append(" ");
        }
    }

    return usersNames;
}

Whenever I try to run the program, I get the following error:

每当我尝试运行该程序时,都会出现以下错误:

non-standard syntax; use '&' to create a pointer to member

Cant find out how to fix it.

无法找出如何修复它。

回答by Julian Declercq

If you use

如果你使用

if (usersList[i]->getUsername() != excludeUser->getUsername)

instead of

代替

if (usersList[i]->getUsername() != excludeUser->getUsername())

your compiler will think you want to use a function pointer instead of the method itself, and if you would have wanted to use a function pointer, you would still have to get the address of it (using &).

您的编译器会认为您想使用函数指针而不是方法本身,如果您想使用函数指针,您仍然需要获取它的地址(使用 &)。

So make sure you don't forget your () after a function call!

因此,请确保在函数调用后不要忘记您的 ()!

回答by SurvivalMachine

You are missing the function call parentheses in these lines:

您在这些行中缺少函数调用括号:

if (usersList[i]->getUsername() != excludeUser->getUsername) {

and

usersNames.append(usersList[i]->getUsername);

Try changing them into this:

尝试将它们更改为:

if (usersList[i]->getUsername() != excludeUser->getUsername()) {

and

usersNames.append(usersList[i]->getUsername());