Home > Software design >  How to get rid of incompatible c conversion
How to get rid of incompatible c conversion

Time:04-12

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)

CodePudding user response:

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.

CodePudding user response:

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?

  • Related