即使我重载了分配,也没有可行的重载'='错误
No viable overloaded '=' error even when I overloaded the assignment
几乎
已经问了确切的问题,但我认为我的问题不是很相似。我将在下面解释代码:
class Person{
public:
string name;
int age, height, weight;
Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
this->name = name;
this->age = age;
this->height = height;
this->weight = weight;
}
void operator = (const Person &P){
name = P.name;
age = P.age;
height = P.height;
weight = P.weight;
}
friend ostream& operator<<(ostream& os, const Person& p);
};
class Stack{
public:
int top;
Person* A;
int size;
Stack(int s){
top = -1;
size = s;
A = new Person[size];
}
bool isEmpty(){
if(top == -1)
return true;
else
return false;
}
bool isFull(){
if(top >= size-1)
return true;
else
return false;
}
void Push(Person* P){
if(isFull()){
cout << "No Space on Stack" << endl;
return;
}
top++;
A[top] = P;
}
};
在代码底部A[top] = P;
行上,我收到错误No viable overloaded '='.
我不明白为什么这不起作用。我在 Person 类中为赋值编写了重载函数,并且我设法更早地重载<<
。我是C++新手,重载是一个非常新的概念,但我无法弄清楚为什么会抛出此错误。
如何解决?
您只定义了operator =
Person
,但您尝试将指针Person*
。未定义执行此类操作的运算符,因此您遇到了错误。
要修复,有一些选项取决于预期的使用情况。
- 在分配之前取消引用指针
- 将
Push
的参数更改为复制或引用Person
,而不是指针 - 添加需要
Person *
class Person
operator =
相关文章:
- 数组索引重载错误
- C++:需要帮助了解运算符重载错误
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 运算符重载错误无匹配运算符<<
- 即使我重载了分配,也没有可行的重载'='错误
- 查找运算符重载C++错误
- 运算符重载错误"no match for operator error"
- 简单3x3矩阵类的运算符重载错误
- 运算符重载错误C++
- 运算符重载错误
- C++运算符">>"重载错误
- 如何使 g++ 运算符重载错误更简洁
- 运算符重载">>"错误
- C++中的重载错误[按值传递和按引用传递]
- 模板的 MSVC 重载错误,但不是 g++
- 运算符+运算符重载错误不匹配
- 函数指针重载错误
- 赋值运算符重载 -- 错误 C2440:'==':无法从 'A<T> *' 转换为 'A<T> *const '
- 引用计数器实现,= 运算符重载错误
- C++运算符重载错误