c++ - 如何在命名空间中使用 += 函数

标签 c++ syntax namespaces overloading

如果函数是在命名空间中声明的,如何正确使用这个“+=”函数?

namespace WinFile
{
     std::stack <tstring> operator += ( std::stack <tstring>& s1, std::stack <tstring> s2 )
     {
        // Post:

     if ( s1 == s2 )
     {
       return s1;
     }

        while ( !s2.empty() )
        {
           s1.push( s2.top() );
           s2.pop();
        } 

        return s1;
   }
}

现在我该如何使用这个函数(不说使用命名空间 WinFile):

std::stack <tstring> s1;
std::stack <tstring> s2;
// ...after adding some values to the stacks
s1 += s2;                // this gets a compile error
s1 WinFile::+= s2        // this says its invalid to have a ':' infront of a +=

最佳答案

如前所述,您可以使用“using”子句:

using WinFile::operator+=;

using namespace WinFile;

或者您可以通过以下代码直接使用该函数:

s1 = WinFile::operator +=( s1, s2 );

这些都不是特别理想,但据我所知,没有其他方法可以做到这一点。

关于c++ - 如何在命名空间中使用 += 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7764323/

相关文章:

使用rust 中意外标记 `(' 附近的语法错误

python - 在 python 2.7 中导入 nltk 的语法无效

javascript - 是否有相当于 Ruby 语法的 Javascript 使用下划线(例如 10_000 = 10000)来使更大的整数易于人类阅读?

.net - 在.Net中添加具有父节点命名空间的XmlNode

c++ - 使用 ntdll.lib 中的 NtCreateSection

c++ - 着色器上带有卷积矩阵的高斯模糊

更改节点 libxml2 的命名空间

xslt - 如何从 xslt 的 xml 输出中删除不需要的空 xmlns

c++ - 以四种格式打印日期

c++ - 有没有办法在 Catalina 上安装 Valgrind?