【发布时间】:2015-02-27 02:19:18
【问题描述】:
我正在处理一项任务,该任务要求在构造带指针的类时编写 Big 3 的函数实现。我似乎在使用复制构造函数时遇到了问题。我认为我做的其他 2 正确。
class RealBox
{
private:
float* m_reals; // Array of Real Numbers
int m_boxsize; // number of Real Numbers in this box
public:
// Purpose: Constructs an Real-Box
// Preconditions:
// 's' is greater than 0;
// Postconditions:
// m_reals points to a dynamically allocated array of size 's'
// all elements of m_reals[] are initiallized to 'a'.
RealBox(int s, float a);
/*
* --------- Big 3 Member Functions -----------
*/
// Purpose: Destructor
// Postconditions: m_reals[] deallocated
~RealBox();
// Purpose: Operator=, performs a deep copy of 'rhs' into 'this' RealBox
// Parameters: rhs, RealBox to be copied
// Returns: *this
// Postconditions: *this == rhs
const RealBox& operator=(const RealBox& rhs);
// Purpose: Copy Constructor
// Parameters: rhs - RealBox to be copied
// Postconditions: *this == rhs
RealBox(const RealBox& rhs);
/*
* ----- Simple Accessor Operations -----
*/
// Purpose: Sets a value in the RealBox
// Parameters: 'i' location to set
// 'x' value to store
// PreConditions: 'i' is between the boundaries of the RealBox
// Postconditions: element 'i' in the RealBox is set to 'x'
void set( int i, float x);
/*
* ----- Complex Accessor Operations -----
*/
// Purpose: prints the RealBox
friend std::ostream& operator<< (std::ostream& out,
const RealBox& box);
}; // RealBox
#include "realbox.h"
#include <iostream>
using namespace std;
RealBox::RealBox(int s, float a)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
{
m_reals[i] = a;
}
}
RealBox::~RealBox()
{
delete [] m_reals;
m_reals = NULL;
}
const RealBox& RealBox::operator =(const RealBox& rhs)
{
if(this != &rhs)
*this = rhs;
return(*this);
}
RealBox::RealBox(const RealBox& rhs)
{
m_reals = new float[m_boxsize];
*this = rhs.m_reals;
}
void RealBox::set(int i, float x)
{
m_reals[i] = x;
}
std::ostream& operator<< (std::ostream& out, const RealBox& box)
{
out <<"[ ";
for (int i = 0; i < box.m_boxsize; i++)
{
out << box.m_reals[i] << ", ";
}
out <<"]"<< endl;
return(out);
}
当我尝试编译时,我收到了错误:
realbox.cpp:在复制构造函数“RealBox::RealBox(const RealBox&)”中: realbox.cpp:33:8: error: no match for ‘operator=’(操作数类型是 ‘RealBox’ 和 ‘float* const’)
*this = rhs.m_reals; ^ realbox.cpp:33:8: 注意:候选人是:
realbox.cpp:23:16: 注意:const RealBox& RealBox::operator=(const RealBox&) const RealBox& RealBox::operator =(const RealBox& rhs) ^ realbox.cpp:23:16: 注意:没有已知的参数 1 从‘float* const’到‘const RealBox&’的转换
我认为我的 operator = 过载也有问题,但我不确定到底出了什么问题。在我开始尝试修复之前,它没有给我那个错误。它试图说我正在比较两种不同的数据类型,但我不确定这是怎么发生的。
【问题讨论】:
-
这些代码 sn-ps 显然不是可运行的 HTML/JavaScript。实际上,在 2015 年,你应该遵循 零 的规则,而不是三的规则。给自己一个可爱的
std::vector并退出所有这些手动内存管理。
标签: c++ constructor deep-copy