gpt4 book ai didi

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

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

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

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/

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