忽略g++编译错误以获得向后兼容性的额外资格

ignore g++ compile error for extra qualification for backwards compatibility

本文关键字:兼容性 编译 g++ 错误 忽略      更新时间:2023-10-16

我正在尝试将一个项目移植到一个新的g++版本的Linux系统中。在编译时,我得到了以下内容:

错误:成员"getCustomer"上的额外资格"Customer::"

在类定义中,我在getCustomer()前面加上Customer::。

如果我删除Customer::,我的代码就可以工作,但是代码中有很多以类名和作用域运算符为前缀的条目。有没有一种方法(如编译器指令)可以帮助消除这个错误?

从我的shellgcc版本4.4.2 20091027(Red Hat 4.4.2-7)(gcc)

在类定义中,我在getCustomer()前面加上Customer::。

我想你的意思是:

class Customer {
    Customer *Customer::getCustomer() { ... }
};

不要。没有必要,因为你已经在类定义中了,我认为C++标准甚至不允许这样做(我很惊讶旧的G++会这样做?)。

似乎没有-std标志(在GCC 4.4.5中)允许这样做。

这样的代码可以用-fpermissive编译(GCC 4.9.2,而非clang 3.6.0)

我根据larsmans的解释测试了完整的程序:

#include <cstdio>
class Customer {
    public: Customer *Customer::getCustomer() { return this; }
};
int main()
{
    std:printf("%pn", Customer().getCustomer());
}

它没有使用g++ test.cpp进行编译,但使用g++ test.cpp -fpermissive进行了编译(带有警告)。甚至CCD_ 5和CCD_。

有了-w,你不会得到警告,但也许这并不总是最好的主意:当有什么奇怪的事情时,你可能想得到通知。

clang++ test.cpp -fpermissive给出了一个错误,尽管clang似乎能识别参数-fpermissive。也许有人知道如何让它接受这样的代码。