gpt4 book ai didi

c++ - vs2010中的函数错误

转载 作者:行者123 更新时间:2023-11-28 07:59:05 24 4
gpt4 key购买 nike

我在 vs2010 中写了以下内容:

int test() const;

它告诉我 const 使用不正确。并说:

Non member function does not allow the use of type qualifier

请问是什么原因,是vs的问题吗?还是vs中怎么使用这样的功能?

最佳答案

const 应用于函数时仅适用于非静态成员函数,不适用于自由函数或静态成员函数。

class A
{
void f1() const; // OK
static void f2() const; // Not OK
};

void f3() const; // Not OK

来自 C++ 标准:

  • 9.3.1 非静态成员函数

A non-static member function may be declared const, volatile, or const volatile.

  • 9.4.1 静态成员函数

A static member function shall not be declared const, volatile, or const volatile.

关于c++ - vs2010中的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com