0
votes

EDITED more code that reproduces the error, changed map to unordered_map.

I've been away and coding pure objective C for over a year now so please forgive me if this is a newbie mistake!

I'm compiling as .mm (Objective-C++) in XCode 5 targeting iOS 7 simulator. It uses clang-503.0.40. based on LLVM 3.4.

Here's the code:

typedef uint32_t uint32;

class Actor {
public:
    std::string name;
    std::set<uint32> behaviors;

    Actor() { }
private:
};

class Location {
public:
    // .. some stuff ...
    std::unordered_map<uint32,std::shared_ptr<Actor>> actors;    // A list of actors in the location

    Location() {  }
private:
};

Then in a function I do -

std::shared_ptr<Location> l1(new Location);
std::shared_ptr<Actor> npc(new Actor);
l1->actors.emplace(npc); // ERROR HAPPENS HERE

I changed it to an unordered map since I couldn't provide a "less" and don't need ordering.

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/unordered_map:544:11: No matching constructor for initialization of 'value_type' (aka 'pair')

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/memory:1641:31: In instantiation of function template specialization 'std::__1::__hash_value_type >::__hash_value_type &>' requested here

There's a whole long chain of candidate constructors that were ruled out and other things like that if you need to see them.

So what newbie mistake am I making?

1
Can you give more details on both the code and the error? - sfjac
@sfjac Which more details? I don't think there's needed more. But I'm fairly no objective-c++ expert. - πάντα ῥεῖ
Don't know - enough to reproduce. There is nothing wrong with the C++ and even if I put this in a .mm file (I'm a C++ guy, not ObjC) everything compiles. - sfjac
Details added, I haven't debugged C++ with XCode in forever, forgot how to find the exact line it happened on. Code now reproduces error - std''OrgnlDave
If you'd given the failing line earlier you'd have got a faster answer. - Alan Stokes

1 Answers

1
votes

Emplace for a map needs one argument for the key, and then subsequent arguments for the thing being emplaced. So

l1->actors.emplace(npc);

cannot work because it only provides one argument - there's no key. You don't really need emplace at all, since you have already constructed the thing you want in the map. You need to specify what key you want to put the element at, and then your simplest option would be

l1->actors[42] = npc;