如果函数是在命名空间中声明的,如何正确使用这个“+=”函数?
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/