【发布时间】:2021-03-01 08:24:22
【问题描述】:
您好,我的问题(至少看起来是这样)是我在声明变量时不能使用重载赋值运算符。我只能在对象已经包含一些值时使用它。
在我的测试文件中,我有以下代码行,它们没有按需要使用赋值运算符,并且会导致程序崩溃。
JumblePuzzle jp2("foo", "easy");
JumblePuzzle jp1 = jp2;
cout << "Attempted assignment" << endl;
但是,以下几行将起作用。
JumblePuzzle jp2("foo", "easy");
JumblePuzzle jp1("bar", "easy");
jp1 = jp2;
cout << "Attempted assignment" << endl;
JumblePuzzle 类
class JumblePuzzle{
public:
JumblePuzzle(string word, string difficultyString);
~JumblePuzzle();
JumblePuzzle(const JumblePuzzle &puzzle);
charArrayPtr* getJumble() const;
charArrayPtr* Jumble() const;
int getSize() const;
int getRowPos() const;
int getColPos() const;
char getDirection() const;
bool placeWord();
string getWord() const;
JumblePuzzle& operator=(const JumblePuzzle &puzzle); //relevant
private:
int size;
char direction;
string word;
int rowPos;
int colPos;
char** jumble;
};
JumblePuzzle 构造函数,我唯一遇到的其他问题是内存分配给 jumble。
JumblePuzzle::JumblePuzzle(string word, string difficultyString){
if(!difficultyString.compare("easy"))
size = word.length() * 2;
else if(!difficultyString.compare("medium"))
size = word.length() * 3;
else if(!difficultyString.compare("hard"))
size = word.length() * 4;
this->word = word;
rowPos = 0;
colPos = 0;
direction = '\0';
jumble = (char**) malloc(size*sizeof(char *));
for(int i = 0; i < size; i ++)
jumble[i] =(char*) malloc(size*sizeof(char));
for(int i = 0; i < size; i ++){
for(int j = 0; j < size; j ++){
jumble[i][j] = '\0';
}
}
while(!this->placeWord());
}
JumblePuzzle 复制构造函数
JumblePuzzle::JumblePuzzle(const JumblePuzzle &puzzle){
size = puzzle.getSize();
rowPos = puzzle.getRowPos();
colPos = puzzle.getColPos();
direction = puzzle.getDirection();
word = puzzle.getWord();
const charArrayPtr *tempJumble = puzzle.Jumble();
jumble = (charArrayPtr*) malloc(size*size*sizeof(char));
memcpy(jumble, tempJumble, size*size);
}
重载的赋值运算符
JumblePuzzle& JumblePuzzle::operator=(const JumblePuzzle &puzzle){
cout << "in assignment operator" << endl;
size = puzzle.size;
direction = puzzle.direction;
rowPos = puzzle.rowPos;
colPos = puzzle.colPos;
word = puzzle.word;
jumble = (charArrayPtr*) malloc(size*sizeof(char *));
for(int i = 0; i < size; i ++)
jumble[i] = (char*) malloc(size*sizeof(char));
jumble = puzzle.jumble;
return *this;
}
此代码用于学校作业,我的教授不希望我们更改测试文件代码,这就是为什么我需要第一段代码才能工作。
我已经尝试寻找解决方案很长一段时间了,如果有人能提供一些见解,我将不胜感激。
【问题讨论】:
-
尽管使用了
=字符,但初始化不是赋值。初始化创建一个对象,赋值替换已经存在的对象的值。 (C++ 中有许多符号和关键字具有多种含义,具体取决于它们出现的位置。)
标签: c++