C++ 找不到命名空间外的函数 [英] C++ can't find function out of namespace

查看:128
本文介绍了C++ 找不到命名空间外的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译以下代码失败,因为第二个函数找不到第一个函数,即使它在命名空间之外.我自己无法弄清楚问题所在,到目前为止我还没有在网上找到任何答案.

Compiling the following code fails because the second function can't find the first one, even though it's outside namespaces. I couldn't figure out the problem myself, and so far I haven't found any answers on the net.

test.cpp:

#include <bits/stdc++.h>

struct myclass {};

template <typename T, typename U>
std::ostream& operator<< (std::ostream &os, const std::pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

namespace my {
    void operator<< (std::ostream os, myclass m) {
        std::cout << std::pair<int, int>(5, 4); // This is line 13.
    }
}

int main() {
    return 0;
}

编译器给出的错误(g++ test.cpp -O2 -o test.exe):
test.cpp:13:13: 错误:'operator<<' 不匹配(操作数类型是std::ostream {aka std::basic_ostream}"和std::pair").
它继续下去,就 operator<< 可能意味着什么给出了一长串建议.

Error given by the compiler (g++ test.cpp -O2 -o test.exe):
test.cpp:13:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::pair<int, int>').
And it goes on, giving a long list of suggestions for what operator<< could have meant.

观察1:如果两个函数名称不同,则不会发生错误.
观察2:如果删除namespace my { },则不会发生错误.

Observation 1: If the two functions differ in name, no error occurs.
Observation 2: If namespace my { } is removed, no error occurs.

推荐答案

这是一种名称隐藏;函数/运算符不能通过不同的作用域进行重载.

This is a kind of name hiding; functions/operators can't be overloaded through different scopes.

根据姓名查找的规则,

(强调我的)

...,名称查找按如下所述检查作用域,直到找到至少一个任何类型的声明,此时查找停止并且不再检查其他作用域.

..., name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.

对于这种情况,名字operator<<<是在namespace my的作用域(即它本身)找到的,然后名字查找停止,全局作用域不会'如果不检查,全局 operator<< 将不会被考虑用于以下重载解析.

For this case, the name operator<< is found at the scope of namespace my (i.e. itself), then name lookup stops, the global scope won't be examined, the global operator<< won't be considered for the following overload resolution.

还有

观察1:如果两个函数名称不同,则不会发生错误.

Observation 1: If the two functions differ in name, no error occurs.

很好,因为没有隐藏名称.

It's fine because there's no name hiding.

观察 2:如果名称空间 my { } 被删除,则不会发生错误.

Observation 2: If namespace my { } is removed, no error occurs.

这很好,因为两个 operator<<< 放在同一个作用域,即全局命名空间.那么 operator<< 都可以找到,然后在重载决议中考虑,最后会选择合适的.

It's fine because the two operator<< is put at the same scope, i.e. the global namespace. Then both operator<< could be found and then considered at overload resolution, the appropriate one will be selected at last.

正如评论所建议的,您可以应用using将全局命名空间中的名称引入命名空间my;那么 operator<< 都会被找到,然后在重载决议中被考虑.

As the comments suggested, you can apply using to introduce the names in global namespace into namespace my; then both operator<< will be found and then considered in overload resolution.

这篇关于C++ 找不到命名空间外的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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