如何摆脱不兼容的 c++转换
我在编译过程中遇到以下错误:
Severity Code Description Project File Line Suppression State
Error C2664 'mytest::Test::Test(const mytest::Test &)': cannot convert argument 1 from '_Ty' to 'const mytest::Test &' TotalTest C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility 158
我不知道是什么,所以我将代码放在这里以举例说明正在做的事情:
totaltest.cpp
include <iostream>
#include "Test.h"
using namespace mytest;
int main()
{
std::cout << "Hello World!\n";
new Test();
}
test.h
#pragma once
#include "Test.h"
#include <iostream>
namespace mytest
{
using namespace std;
class Test
{
public:
Test();
~Test();
shared_ptr<Test> t;
};
}
test.cpp
#include "Test.h"
namespace mytest
{
Test::Test()
{
}
Test::~Test()
{
}
}
testfactory.h
#pragma once
#include "Test.h"
#include <iostream>
namespace mytest
{
using namespace std;
class TestFactory
{
public:
TestFactory();
shared_ptr<Test> CreateTest(int testClass);
};
}
testfactory.cpp
#include "TestFactory.h"
namespace mytest
{
TestFactory::TestFactory()
{
}
shared_ptr<Test> TestFactory::CreateTest(int testClass)
{
return make_shared<Test>(new Test());
}
}
我正在使用Visual Studio C ++语言标准:ISO C ++ 14标准(/性病:C ++ 14)
I'm getting the following error during compilation:
Severity Code Description Project File Line Suppression State
Error C2664 'mytest::Test::Test(const mytest::Test &)': cannot convert argument 1 from '_Ty' to 'const mytest::Test &' TotalTest C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility 158
I have no idea what is, so I'm putting the code here to exemplify what was being done:
TotalTest.cpp
include <iostream>
#include "Test.h"
using namespace mytest;
int main()
{
std::cout << "Hello World!\n";
new Test();
}
Test.h
#pragma once
#include "Test.h"
#include <iostream>
namespace mytest
{
using namespace std;
class Test
{
public:
Test();
~Test();
shared_ptr<Test> t;
};
}
Test.cpp
#include "Test.h"
namespace mytest
{
Test::Test()
{
}
Test::~Test()
{
}
}
TestFactory.h
#pragma once
#include "Test.h"
#include <iostream>
namespace mytest
{
using namespace std;
class TestFactory
{
public:
TestFactory();
shared_ptr<Test> CreateTest(int testClass);
};
}
TestFactory.cpp
#include "TestFactory.h"
namespace mytest
{
TestFactory::TestFactory()
{
}
shared_ptr<Test> TestFactory::CreateTest(int testClass)
{
return make_shared<Test>(new Test());
}
}
I'm using Visual Studio C++ language standard: ISO C++14 Standard (/std:c++14)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
TestFactory::CreateTest()
中,make_shared(new Test())
是错误的,因为Test
没有一个构造函数接受Test*
指针作为输入。您需要使用
make_shared()
来代替,让make_shared()
为您调用默认的Test()
构造函数:您指定的任何参数传递给
make_shared()
的内容被传递给模板参数中指定类型的构造函数。在这种情况下,不需要任何参数。In
TestFactory::CreateTest()
,make_shared<Test>(new Test())
is wrong, asTest
does not have a constructor that accepts aTest*
pointer as input.You need to use
make_shared<Test>()
instead, lettingmake_shared()
call the defaultTest()
constructor for you:Any parameters you pass to
make_shared()
are passed to the constructor of the type specified in the template argument. In this case, there are no parameters needed.错误来自以下行:
有两种方法可以初始化
std :: shardy_ptr
:直接使用
std :: shared_ptr
构造函数直接使用,这需要您传递测试
已经分配在堆上的对象,例如:使用
make_shared() /code>,内部执行堆分配,例如:
在括号中,您可以将参数传递到
test test
的构造函数。。
通常优选第二个选项。您可以在此处查看更多信息: Make_shared中的差异和C ++中的正常shared_ptr
另外:
test.h
不应包括自己(它具有#include“ test.h”
的顶部)。您应该避免使用
使用命名空间std;
。更多信息在这里:为什么“使用命名空间std;”被认为是不良练习吗?The error comes from the following line:
There are 2 ways of initializing a
std::shared_ptr
:Using the
std::shared_ptr
constructor directly, which requires you to pass aTest
object already allocated on the heap, e.g.:Using
make_shared()
, which performs the heap allocation internally, e.g.:Where in the parentheses you can pass parameters to the constructor of
Test
.The 2nd option is usually prefered. You can see more info here: Difference in make_shared and normal shared_ptr in C++
In addition:
Test.h
should not include itself (it has#include "Test.h"
at the top).You should avoid using
using namespace std;
. More info here: Why is "using namespace std;" considered bad practice?