gpt4 book ai didi

c++ - using 指令如何影响 C++ 中的函数参数?

转载 作者:太空狗 更新时间:2023-10-29 23:08:22 25 4
gpt4 key购买 nike

我有以下代码,使用 g++ 4.4.6 可以正常工作,但无法使用 Visual Studio 2008 进行编译。它似乎与参数相关查找有关,所以我认为 g++ 是正确的。

// testClass.hpp
namespace test {
class foo {
public:
foo(){}
};

class usesFoo {
public:
usesFoo() {}

void memberFunc(foo &input);
};
}

// testClass.cpp
#include "testClass.hpp"

using test::usesFoo;

void usesFoo::memberFunc(foo &input) {
(void) input;
}

我在 Visual Studio 中编译时遇到的错误是,

1>正在编译...
1>测试类.cpp1>c:\work\testproject\testproject\testclass.cpp(6) : error C2065: 'foo' : 未声明的标识符1>c:\work\testproject\testproject\testclass.cpp(6) : error C2065: 'input' : 未声明的标识符1>c:\work\testproject\testproject\testclass.cpp(6) : error C2448: 'test::usesFoo::memberFunc' : 函数式初始化程序似乎是一个函数定义

我意识到要么将命名空间直接放在 cpp 文件中的成员函数上,要么“使用命名空间测试”将解决问题,我更好奇标准在这种情况下到底说了什么。

最佳答案

代码是正确的,但它与参数依赖查找无关。此外,using 声明仅影响 usesFoo 而不是 foo 的查找:一旦您说出类成员的名称,就会在此类的上下文中查找其他名称.因为 foo 是它找到的 test::usesFoo` 的成员。如果没有 using 指令,您需要像这样定义成员函数:

void test::usesFoo::memberFunction(foo& input) {
(void)input;
}

与此相关的条款是 3.4.1 不合格名称查找 [basic.lookup.unqual] 第 6 段:

A name used in the definition of a function following the function’s declarator-id that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in which it is used or in one of its enclosing blocks (6.3) or, shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N’s enclosing namespaces.

参数相关查找仅在调用函数时进入画面,而不是在定义时进入画面。这些事情彼此根本没有任何关系。

关于c++ - using 指令如何影响 C++ 中的函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879719/

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