Reputation: 2028
I am experiencing trouble with the compilation of my C++. Namely, I get the following obscure error from the header:
In file included from .../map.test.cpp:1:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2172:15: error: no matching constructor for initialization of
'SmallWorld::Map::Graph<SmallWorld::Region>'
__second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
The code in question is the following:
#include <memory>
#include <string>
#include <gtest/gtest.h>
#include "../../../app/Domain/Map/loader.cpp"
using std::string;
TEST(creates_a_connected_graph, small_world_map){
using SmallWorld::Region;
using Graph = SmallWorld::Map::Graph<Region>;
using search_set = std::set<string>;
using forest_map = std::unordered_map<string, std::pair<string, string>>;
std::shared_ptr<Graph> g = SmallWorld::Map::loadMap("./map.json");
const size_t order = g->order();
const size_t dfs_order = SmallWorld::Map::algorithm::dfs<Region>(
"r1",
[](const string&, const search_set&, const forest_map&) -> bool { return true; }
)(*g);
ASSERT_EQ(order, dfs_order);
};
where the faulty line is most likely at the shared_ptr instantiation. The call to loadMap calls the following function:
std::shared_ptr<Map::Graph<Region>> loadMap(const string& path, const string& schema = "./map.ajv.json") {
using Graph = SmallWorld::Map::Graph<Region>;
using region_ptr = std::shared_ptr<Region>;
json* map = readMap(path, schema);
std::vector<std::pair<string, region_ptr>> regions = extractRegions(*map);
std::vector<std::pair<string, string>> edges = extractEdges(*map);
std::shared_ptr<Graph> pgraph = std::make_shared<Graph>(new Graph(regions, edges));
delete map;
return pgraph;
};
And my only user-defined constructor of the Graph class:
Graph<E>(std::vector<std::pair<string, std::shared_ptr<E>>> nodes, std::vector<std::pair<string, string>> edges);
Setup:
I've added the verbose flag to my CMAKE_CXX_FLAGS to see if I could get something like a stack trace for the compilation error, but to no avail. This most likely is a duplicate. If you have an idea of how to solve this issue, it would be great; if you know a way to get stack-trace like errors for compilation errors, you would be my savior <3
🚀
Upvotes: 2
Views: 5726
Reputation: 40882
Use std::make_shared<Graph>
the same way you would use new Graph
, as of that it has to be:
std::make_shared<Graph>(regions, edges);
The std::make_shared<Graph>(new Graph(regions, edges));
is as if you would write new Graph( new Graph(regions, edges) )
.
What you may have seen and meant is std::shared_ptr<Graph>(new Graph(regions, edges));
, but that's a construct you should not use.
Upvotes: 3