对于我的异常类,我有一个具有多个参数 (...) 的构造函数,它在 Windows 下运行良好,但是在 Linux 下它编译正常但拒绝链接到它。
为什么这在 linux 下不起作用?
这是一个例子:
class gcException
{
public:
gcException()
{
//code here
}
gcException(uint32 errId, const char* format = NULL, ...)
{
//code here
}
}
enum
{
ERR_BADCURLHANDLE,
};
.
编辑
所以当我这样调用它时:
if(!m_pCurlHandle)
throw gcException(ERR_BADCURLHANDLE);
我得到这个编译错误:
error: no matching function for call to ‘gcException::gcException(gcException)’
candidates are: gcException::gcException(const gcException*)
gcException::gcException(gcException*)
gcException::gcException(gcException&)
最佳答案
问题是你的复制构造函数不接受你抛出的临时值。这是一个临时的,因此是一个右值。对非常量的引用,即 gcException&
不能绑定(bind)到它。阅读here关于细节。
正如对该答案的评论所暗示的那样,微软编译器有一个错误,导致它绑定(bind)指向非常量对象的引用接受右值。您应该将复制构造函数更改为:
gcException(gcException const& other) {
// ...
}
让它发挥作用。它说该错误已在 Visual C++ 2005 中修复。因此从该版本开始,您会遇到同样的问题。所以最好马上解决这个问题。
关于c++ - 为什么构造函数中的多参数在 linux 下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/464143/