1

I seem to be getting several errors with

map<string,function<XMLSerializable*()>> mapConstructor;

Notably,

 la5.cpp: In function ‘int main(int, char**)’:
 la5.cpp:21:13: error: ‘function’ was not declared in this scope
 la5.cpp:21:43: error: ‘mapConstructor’ was not declared in this scope
 la5.cpp:21:43: error: template argument 2 is invalid
 la5.cpp:21:43: error: template argument 4 is invalid
 la5.cpp:25:58: warning: lambda expressions only available with -std=c++0x or - std=gnu++0x [enabled by default]
 la5.cpp:33:26: error: expected primary-expression before ‘*’ token
 la5.cpp:33:28: error: expected primary-expression before ‘)’ token
 la5.cpp:33:31: error: ‘pFunc’ was not declared in this scope
 make: *** [la5.o] Error 1

Unfortunately, I can't seem to find what I've done wrong, as it seems to deal with that map declaration which was given to the class by my instructor. Below is my .cpp

#include <iostream>
#include <map>
#include <string>
#include <functional>

#include "Armor.h"
#include "Weapon.h"
#include "Item.h"
#include "Creature.h"

using namespace std;

XMLSerializable * constructItem()
{
        return new Item;
}

int main(int argc, char * argv[])
{

    map<string,function<XMLSerializable*()>> mapConstructor;

    mapConstructor["Item"] = constructItem;

    mapConstructor["Creature"] = []() {return new Creature; };

    cout << "Input the class name, then we'll try to construct it." << endl;

    string sLookup = " ";

    cin >> sLookup;

    function<XMLSerializable*()> pFunc = mapConstructor[sLookup];

    if(pFunc() == NULL)
    {
            cout << "Sorry, the object couldn't be constructed." << endl;
    }
    else
    {
            cout << pFunc() << " a non NULL value was returned!" << endl;
    }
    return 0;
}

Any suggestions? I'm unfamiliar with maps, but I believe this should work, right?

Coding in pico, compiling with a makefile using g++.

1
  • std::function isn't a function pointer, it's a wrapper class for all sorts of callables, including function pointers. Commented Oct 4, 2013 at 3:22

1 Answer 1

1

It looks like you just are forgetting to add -std=c++11 or -std=c++0x to your compiler flags to enable C++11.

-std=c++0x is deprecated, but on older versions of g++, -std=c++11 is not available.

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I thought as well, but the prompt had only said to put the -std=c++0x in a certain spot, so I didn't question it. Trying with the c++0x standard everywhere compiled just fine, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.