如何摆脱不兼容的 c++转换

发布于 2025-01-20 15:18:48 字数 1732 浏览 3 评论 0原文

我在编译过程中遇到以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殤城〤 2025-01-27 15:18:48

TestFactory::CreateTest() 中,make_shared(new Test()) 是错误的,因为 Test 没有一个构造函数接受 Test* 指针作为输入。

您需要使用 make_shared() 来代替,让 make_shared() 为您调用默认的 Test() 构造函数:

shared_ptr<Test> TestFactory::CreateTest(int testClass)
{
    return make_shared<Test>();
}

您指定的任何参数传递给 make_shared() 的内容被传递给模板参数中指定类型的构造函数。在这种情况下,不需要任何参数。

In TestFactory::CreateTest(), make_shared<Test>(new Test()) is wrong, as Test does not have a constructor that accepts a Test* pointer as input.

You need to use make_shared<Test>() instead, letting make_shared() call the default Test() constructor for you:

shared_ptr<Test> TestFactory::CreateTest(int testClass)
{
    return make_shared<Test>();
}

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.

泪之魂 2025-01-27 15:18:48

错误来自以下行:

return make_shared<Test>(new Test());

有两种方法可以初始化std :: shardy_ptr

  1. 直接使用std :: shared_ptr构造函数直接使用,这需要您传递测试已经分配在堆上的对象,例如:

      std :: shared_ptr&lt; test&gt; p {new Test()};
     
  2. 使用make_shared() /code>,内部执行堆分配,例如:

      std :: shared_ptr&lt; test&gt; p {std :: make_shared&lt; test&gt;()};
     

    在括号中,您可以将参数传递到test test的构造函数。


通常优选第二个选项。您可以在此处查看更多信息: Make_shared中的差异和C ++中的正常shared_ptr

另外:

  1. test.h不应包括自己(它具有#include“ test.h”的顶部)。

  2. 您应该避免使用使用命名空间std;。更多信息在这里:为什么“使用命名空间std;”被认为是不良练习吗?

The error comes from the following line:

return make_shared<Test>(new Test());

There are 2 ways of initializing a std::shared_ptr:

  1. Using the std::shared_ptr constructor directly, which requires you to pass a Test object already allocated on the heap, e.g.:

     std::shared_ptr<Test> p{ new Test() };
    
  2. Using make_shared(), which performs the heap allocation internally, e.g.:

     std::shared_ptr<Test> p{ std::make_shared<Test>() };
    

    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:

  1. Test.h should not include itself (it has #include "Test.h" at the top).

  2. You should avoid using using namespace std;. More info here: Why is "using namespace std;" considered bad practice?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文