Compiler error C2664 Cannot convert argument with std::make_shared

1.4k views Asked by At

This is rather perplexing; I have a class:

namespace myApp {
    class WorldData {
        public:
            WorldData(std::string& environmentFile, bool isProduction = false);
            ...
            ...
    };
}
#include "myApp.h"

using namespace myApp;

WorldData::WorldData(std::string& environmentFile, bool isProduction) {
    mIsProduction = isProduction;
    // more logic with the environmentFile
}

and I tried to create the shared pointer with std::make_shared:

    std::string environmentFile = "data/environment/hall.json";

    mWorldData = std::make_shared<myApp::WorldData>(environmentFile, true);

and this gives me an error:

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'myApp::WorldData::WorldData(std::string &,bool)': cannot convert argument 1 from '_Ty' to 'std::string &'  MyApplication   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility    158 

But this seems not quite right because my first argument is a string! I've tried wrapping it in std::string, but that doesn't seem quite right, so I'm convinced there's something a bit trickier going on here.

Edit : copied full error message below:

2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(158,56): error C2664: 'myApp::WorldData::WorldData(const std::string &,bool)': cannot convert argument 1 from '_Ty' to 'const std::string &'
2>        with
2>        [
2>            _Ty=myApp::WorldData *
2>        ] (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(151,49): message : Reason: cannot convert from '_Ty' to 'const std::string'
2>        with
2>        [
2>            _Ty=myApp::WorldData *
2>        ] (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(151,49): message : No constructor could take the source type, or constructor overload resolution was ambiguous (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>D:\Projects\myProject\dependencies\my-project\src\simulation\WorldData.cpp(15,12): message : see declaration of 'myApp::WorldData::WorldData'
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\memory(2021): message : see reference to function template instantiation 'void std::_Construct_in_place<_Ty,myApp::WorldData*>(_Ty &,myApp::WorldData *&&) noexcept(false)' being compiled
2>        with
2>        [
2>            _Ty=myApp::WorldData
2>        ] (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\memory(2747): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<myApp::WorldData*>(myApp::WorldData *&&)' being compiled
2>        with
2>        [
2>            _Ty=myApp::WorldData
2>        ] (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\memory(2748): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<myApp::WorldData*>(myApp::WorldData *&&)' being compiled
2>        with
2>        [
2>            _Ty=myApp::WorldData
2>        ] (compiling source file ..\..\..\..\dependencies\my-project\src\simulation\WorldData.cpp)
2>D:\Projects\myProject\dependencies\my-project\src\simulation\WorldData.cpp(81): message : see reference to function template instantiation 'std::shared_ptr<myApp::WorldData> std::make_shared<myApp::WorldData,myApp::WorldData*>(myApp::WorldData *&&)' being compiled

This seems to suggest that the compiler thinks the constructor is overloaded and can't find the right one? Which is odd to me, because I only have one constructor defined!

1

There are 1 answers

0
Luis Guzman On

I'm hesitantly adding this "answer" with the hope of being useful. I say hesitantly because your question is missing a "minimal reproducible example" and compilation details. That said, I have an example with all of the pieces of your failing code that works, so you should be able to compare it with what you have and see what is failing. I made it simple, and placed all of the code in one file even though it is structured to be in two files. I had to comment out a few lines that I left there for clarity. Anyway, here it is:

//#pragma once

#include <string>

namespace myApp {
    class WorldData {
        public:
            WorldData(std::string& environmentFile, bool isProduction = false);
        private:
            bool mIsProduction;
    };
}
    
//#include "myApp.h"
#include <iostream>
#include <memory>
    
using namespace myApp;

WorldData::WorldData(std::string& environmentFile, bool isProduction) {
    mIsProduction = isProduction;
    // more logic with the environmentFile

    std::cout << environmentFile << "\n";
}

int main()
{
    std::string environmentFile = "data/environment/hall.json";

    std::shared_ptr<myApp::WorldData> mWorldData = std::make_shared<myApp::WorldData>(environmentFile, true);
}

I compiled it using g++ version 4.8.5, but it will compile with MSVC too. Here is the command I used to compile:

$ g++ -std=c++11 onefile.C
$

And the output:

$ ./a.out
data/environment/hall.json
$

I hope it helps. If it does, let us know what was your problem...