1

I am unable to define a map with my own class as the value type. This is exactly what I am trying:

myfile.cpp

class myclass
{
public:

    myclass() {}
    myclass(const myclass &m):y(m.y){}
    ~myclass() {}
    int y;

};

int main()
{
    ...
    std::map<std:string, myclass> funcmap;
    ...
}

This does not compile:

g++ myfile.cpp
myfile.cpp:46:33: error: wrong number of template arguments (1, should be 4)
/usr/include/c++/4.6/bits/stl_map.h:88:11: error: provided for âtemplate<class _Key, class _Tp, class _Compare, class _Alloc> class std::mapâ

I don't get this error if I use an int or string in the place of myclass in the map declaration.

4

1 回答 1

4

You're missing a colon in std::string. You wrote:

std:string

and it should be:

std::string

You can see that this:

#include <map>
#include <string>

class myclass
{
public:

    myclass() {}
    myclass(const myclass &m):y(m.y){}
    ~myclass() {}
    int y;

};

int main()
{
    std::map<std::string, myclass> funcmap;

    return 0;
}

Compiles

于 2013-07-25T06:24:35.210 回答