C ++中的Const成员变量11

时间:2014-10-13 14:41:30

标签: c++ c++11

   category::category ( const std::string p_name , std::string p_ImagePath) :
    m_name { p_name },
    m_ImagePath {p_ImagePath }
    {

    }

#pragma once
#include <string>
class category
{
public:
    const int i;
    explicit category ( const std::string p_name ,const std::string p_ImagePath);
    ~category ( );
    std::string GetName ( );
private:
    std::string m_name;
    std::string m_ImagePath;
};

由于赋值操作符

,我总是得到错误

Fehler 1错误C2280:&#39;预订和预订:: operator =(const booking&amp;)&#39; :尝试引用已删除的函数C:\ Program Files(x86)\ Microsoft Visual C ++编译器2013年11月CTP \ include \ utility 53

如果我尝试在类中使用const成员变量或const静态成员变量。

I tried const i = 5;
static const i = 5;
and const i; -> i gets initialized in constructor.

什么都行不通,我该如何解决这个问题? 我不能使用constexpr,因为vs2013没有帮助它:(

我已经在Stackoverflow上检查过一些问题,但一切都是用constexpr

1 个答案:

答案 0 :(得分:2)

您必须明确定义copy assignment operator。由于您的class有一个constantnon-static数据成员,因此删除了编译器定义的copy assignment operator

还完全不清楚constant定义中non-staticclass数据成员的含义是什么。

对于constructor,您可以删除函数说明符explicit并将参数定义为常量引用。

category( const std::string &p_name, const std::string &p_ImagePath );