模板错误:没有适当的默认构造函数可用 [英] Template Error: no appropriate default constructor available

查看:279
本文介绍了模板错误:没有适当的默认构造函数可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情是,我不是(知道试图使用任何默认构造函数 beatle :: beatle
的错误:

The thing is, I am not (knowing trying to use any default constructor of beatle::beatle the error:

1>  ecosystem.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(172): error C2512: 'beatle::beatle' : no appropriate default constructor available
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(165) : while compiling class template member function 'beatle &std::map<_Kty,_Ty>::operator [](int &&)'
1>          with
1>          [
1>              _Kty=tokenID,
1>              _Ty=beatle
1>          ]
1>          c:\users\zak\documents\visual studio 2010\projects\ascii_sivvure\ascii_sivvure\ecosystem.h(22) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=tokenID,
1>              _Ty=beatle
1>          ]

不相关的代码已删除:

header:

typedef std::map<tokenID,beatle>    Beatles;

class ecosystem
{
private:
line 22:    Beatles m_Beatles;
};

来源:

ecosystem::ecosystem(): m_output( output() )
{
    Beatles m_Beatles;
}

void ecosystem::populate()
{
    if (m_isMatingSeason && ( random(0,1000) < rateMATING ) )
    {
        beatle babyBeatle = breed();
        m_Beatles[babyBeatle.getTokenID()] = babyBeatle;
        m_field.occupy(babyBeatle.getTokenID(), babyBeatle.getLocation() );
    }
}

我一直在使用不同的组合尝试正确地定义/声明 maps 。在一点intellisense开始说它想要指针对象在这里:

I've been trying for hours using different combinations of trying to properly define/declare the maps. At one point intellisense starting saying it wanted pointers to object here:

m_Beatles[babyBeatle.getTokenID()] = babyBeatle;

这导致了我的悲伤。

这是所有发生在我的第一次(并希望最后)重构狂潮,它已经一个多星期,因为我已经能够编译...我可能有40小时只是想让它再次工作。

This is all happening after my first(and hopefully last) refactoring binge, its been over a week since I've been able to compile... I probably have 40 hours just trying to get it working again.

推荐答案

std :: map<> :: operator [] 构造函数。这是因为它首先在地图中创建一个条目,然后执行您正在调用的 operator =

std::map<>::operator[] requires a default constructor. This is because it first creates an entry in the map then does the operator= that you are calling.

你绝对想使用 std :: map ,但不想提供一个默认的构造函数(也许这是不合逻辑的你的情况?)你可以使用:

If you absolutely want to use an std::map but do not want to provide a default constructor (perhaps it is illogical for your case?) you can use:

std :: map<> :: insert()

显式地将对象插入到代码中。这使事情有点复杂,因为查找也必须使用find。

To explicitly insert the object into the code. This makes things a bit more complicated then because look ups must also use find.

我注意到,Adam Rosenfield已经在评论中发布了这个信息,但是将此作为单独的答案。

I just noticed that Adam Rosenfield already posted this info in a comment but going ahead and leaving this as a separate answer.

这篇关于模板错误:没有适当的默认构造函数可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆