结构的默认构造函数,用于正确的std :: map行为 [英] Default constructor of the structure for correct std::map behaviour

查看:50
本文介绍了结构的默认构造函数,用于正确的std :: map行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们需要为默认的 std :: map 行为定义默认的构造函数 TConcrete()?没有它,我得到以下信息:

Why do we need to define the default constructor TConcrete() for the correct std::map behaviour? Without it I got the following:

 >note: see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<std::tuple<std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&>,std::tuple<>,0,>(_Tuple1 &,_Tuple2 &,std::integer_sequence<unsigned int,0>,std::integer_sequence<::size_t>)' being compiled
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=TConcrete,
1>            _Tuple1=std::tuple<std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&>,
1>            _Tuple2=std::tuple<>
1>        ]

这是我的代码.我正在使用 C ++ 03 .

Here is the code I have. I am using C++03.

main.cpp

#include<iostream>
#include"TBuilder.h"

using namespace std;

int main()
{
    TBuilder builder = TBuilder();
    cout << builder.Get_Eb("B25");
    cin.get();
    return 0;
}

TBuilder.h

#pragma once
#include"TConcrete.h"

class TBuilder {
private:
    TConcreteData concrete_data;
public:
         TBuilder();
    double Get_Eb(string);
};

TBuilder.cpp

#include"TBuilder.h"

TBuilder::TBuilder()
{
    TConcrete B25 = TConcrete( "B25",2000,20,2 );
    concrete_data["B25"] = B25;
}

double TBuilder::Get_Eb(string grade0)
{
    return concrete_data[grade0].E_b;
}

TConcrete.h

#pragma once

#include<map>
#include<string>
#include "main.h"

using namespace std;

struct TConcrete {
    string grade;
    double E_b, R_b, R_bt;
    TConcrete();
    TConcrete(string, double,double,double);
};   
typedef map<string, TConcrete> TConcreteData;

TConcrete.cpp

#include "TConcrete.h"

TConcrete::TConcrete()
{
}

TConcrete::TConcrete(string grade0, double E_b0, double R_b0, double Rb_t0)
{
    grade = grade0;
    E_b = E_b0;
    R_b = R_b0;
    R_bt = R_b0;
}   

我阅读了 std :: map在[],在讨论insert()时复制构造函数,但同时使用 insert()也需要默认构造函数.使用 insert()查看代码.

I read std::map calls default constructor on [], copy constructor on insert() discussion but using insert()required the default constructor as well. See the code with insert().

TConcrete.h (已通过 insert()修饰)

#pragma once

#include<map>
#include<string>
#include "main.h"
#include<utility>

using namespace std;

struct TConcrete {
    string grade;
    double E_b, R_b, R_bt;
    TConcrete();
    TConcrete(string, double,double,double);
};
typedef map<string, TConcrete> TConcreteData;
typedef pair<string, TConcrete> TConcreteDataItem;

TBuilder.cpp (已通过 insert()修饰)

#include"TBuilder.h"

TBuilder::TBuilder()
{
    TConcrete B25 = TConcrete( "B25",2000,20,2 );
    concrete_data.insert(TConcreteDataItem("B25",B25));
}

double TBuilder::Get_Eb(string grade0)
{
    return concrete_data[grade0].E_b;
}

推荐答案

由于使用 std :: map 的方式,您需要默认的构造函数,因此可能需要它来创建对象.如果您不以这种方式使用它,则不需要默认构造函数.

You need a default constructor because of the way you are using std::map requires it to possibly create objects. If you don't use it in such a way default constructors are not necessary.

由于缺少默认构造函数,因此无法编译此代码:

This code will not compile due to the lack of default constructors:

#include <map>

struct Struct
{
    Struct(int) {}
};

int main()
{
    std::map<std::string, Struct > m;
    m["1"] = Struct(1);
    Struct& s = m["1"];
}

[] 运算符返回对现有值的引用.如果该值不存在,则会创建一个新值(使用默认构造函数),并返回对该值的引用.第一条语句可能看起来不是这样,但实际上等效于:

The [] operator returns a reference to an existing value. If that value doesn't exist a new one is created (using the default constructor) and a reference to that is returned. The first statement might not look like this is what's going on but is actually equivalent to:

Struct& s = m["1"];
s = Struct(1);

如果使用 find insert ,则不需要默认构造函数:

If you use find and insert instead no default constructor is required:

int main()
{
    std::map<std::string, Struct > m;
    m.insert(std::make_pair(std::string("1"), Struct(1)));
    auto it = m.find("1");
    if (it != m.end())
    {
        Struct& s = it->second;
    }
}

这篇关于结构的默认构造函数,用于正确的std :: map行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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