【发布时间】:2014-09-30 23:34:14
【问题描述】:
#include "booking.h"
#include <iostream>
booking::booking ( const std::string p_title, const std::string p_notice, const category p_category, const person p_person, const booking::Type p_type, const double p_value ) :
m_type{ p_type },
m_title{ p_title },
m_notice{ p_notice },
m_person{ p_person },
m_category{ p_category },
m_value { p_value }
{
std::cout << "Booking was created" << std::endl; // Debug Message
}
这些是文件(我认为必须知道的所有内容)
#pragma once
#include <string>
#include "person.h"
#include "category.h"
class booking
{
public:
enum Type { TYPE_REVENUE, TYPE_EXPENDITURE };
booking ( const std::string p_title, const std::string p_notice, const category p_category, const person p_person, const booking::Type p_type, const double p_value ); //Basic Constructor
~booking();
Type GetType ( );
std::string GetTitle ( );
std::string GetNotice ( );
category GetCategory ( );
double GetValue ( );
private:
Type m_type;
std::string m_title;
std::string m_notice;
category m_category;
person m_person;
double m_value;
};
如果我将类成员之一(如 m_type 或 double 值,不管哪个)放入 const,它会引发以下错误:
Fehler 1 错误 C2280:
booking &booking::operator =(const booking &):试图引用已删除的函数 C:\Program Files (x86)\Microsoft Visual C++ Compiler Nov 2013 CTP\include\utility 53
我不明白为什么编译器会抱怨复制构造函数以及基本上是什么问题。
【问题讨论】:
-
通过 const 引用传递字符串,可能还有你的人和类别。
-
除极少数情况外,我从不将成员变量设为 const。相反,如果我不希望它改变,我将对象本身设为 const。
标签: c++ constructor copy