gpt4 book ai didi

c++ - gcc编译失败,但msc++编译成功

转载 作者:行者123 更新时间:2023-12-03 07:22:00 28 4
gpt4 key购买 nike

我使用gcc编译,版本是Ubuntu 18.0.4 64位中的(Ubuntu 9.3.0-17ubuntu1〜20.04)9.3.0。
可以使用msvc++ 2015成功编译以下代码,但是使用gcc可以成功编译以下代码。


class BaseClass
{
};

template <typename T>
class CPIPtr
{
public:
CPIPtr()
{
_p = nullptr;
}

CPIPtr(const CPIPtr& item)
{
_p = nullptr;
*this = item;
}

CPIPtr(BaseClass* pI)
{
_p = nullptr;
}

~CPIPtr()
{
}

operator T* () const
{
return _p;
}

T* operator=(BaseClass* pI)
{

return _p;
}

private:
// public:
T* _p;
};

#define GF_DEFINE_INTERFACE_REF_PTR(x) \
typedef CPIPtr<x> x ## Ptr

class ITest : public BaseClass
{};

GF_DEFINE_INTERFACE_REF_PTR(ITest);

class ITest1 : public BaseClass
{};

GF_DEFINE_INTERFACE_REF_PTR(ITest1);

int main()
{
#if 0
ITestPtr test;
ITest1Ptr test1;

ITest1Ptr test1 = test;
// the above line code is equal the below two lines with msvc++
ITest* pTest = test.operator ITest *();
Test1.operator=(pTest);

#endif//0

ITestPtr test;
ITest1Ptr test1 = test;
test = test1;

return 0;
}
使用gcc编译器时,发生跟随错误。
错误:请求从“CPIPtr”转换为非标量类型“CPIPtr”。
我该如何解决呢?
提前致谢。

最佳答案

问题出在行中,

ITest1Ptr test1 = test; 
这将调用复制构造函数,但在此之前,它无法进行类型转换,而是使用,
ITest1Ptr test1 = static_cast<ITest1Ptr>(test);
要么,
ITest1Ptr test1 (test);
以下行正在调用赋值运算符,没关系,
test = test1;

关于c++ - gcc编译失败,但msc++编译成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64819262/

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