无法与'operator='匹敌
no match for 'operator='
>我已经构建了一个静态的结构堆栈,一切都可以正常工作 - 包括创建结构数组。但是,由于某种原因,我无法将数组的顶部设置为结构。
在我的.cpp
文件中,在我的push
函数中,我在以下行上不断出错:
stackArray[top] = newStudent;
我收到的错误是:
"51: 与'(((studentstack)this)->studentstack::stackArray + (+(((unsigned int)((studentstack*)this)->studentstack::top) *24u))) = ((studentstack*)this)->studentstack::newStudent' 中的'operator=' 不匹配
我在下面包含我的代码。
头文件:
#ifndef studentstack_H
#define studentstack_H
#include <iostream>
#include <string>
using namespace std;
class studentstack {
private:
int size; // Stack size
int top; // Top of the Stack
struct student {
int ID;
string Name;
string Address;
double GPA;
};
student * stackArray; // Pointer to the stack
student * newStudent; // Pointer to the new student
public: //Constructor
studentstack(int);
// Copy Constructor
studentstack(const studentstack &);
//Destructor
~studentstack();
//Stack Operaations
void push(string, int, double, string);
void pop(int &);
bool isFull() const;
bool isEmpty() const;
};
#endif
学生栈.cpp
#include <iostream>
#include "studentstack.h"
using namespace std;
studentstack::studentstack(int SIZE) {
stackArray = new student[SIZE];
size = SIZE;
top = -1;
int ID = 0;
double GPA = 0;
}
studentstack::studentstack(const studentstack &obj) {
if (obj.size > 0)
stackArray = new student[obj.size];
else
stackArray = NULL;
size = obj.size;
for (int count = 0; count < size; count++)
stackArray[count] = obj.stackArray[count];
top = obj.top;
}
studentstack::~studentstack() {
delete [] stackArray;
}
void studentstack::push(string name, int id, double gpa, string address) {
if (isFull()) {
cout << "The stack is full.n";
} else {
top++;
newStudent = new student;
newStudent-> Name = name;
newStudent-> ID = id;
newStudent-> Address = address;
newStudent-> GPA = gpa;
stackArray[top] = newStudent;
}
}
void studentstack::pop(int &id) {
if (isEmpty()) {
cout << "The stack is empty.n";
} else {
id = stackArray[top].ID;
top--;
}
}
bool studentstack::isFull() const {
bool status;
if (top == size - 1)
status = true;
else
status = false;
return status;
}
bool studentstack::isEmpty() const {
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
主.cpp
#include "studentstack.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int id, var;
string address;
double gpa;
studentstack s[20];
for(int x =0; x<20; x++) {
cout << "New Student Name: " << endl;
cin >> name;
cout << "ID: " << endl;
cin >> id;
cout << "GPA: " << endl;
cin >> gpa;
cout << "Address: " << endl;
cin >> address;
s.push(name, id, gpa, address)
}
cout << "Popping: "
for(int x = 0; x < 5; x++) {
s.pop(var);
cout <<var;
}
return(0);
}
您可能希望将示例缩减为显示问题的最小代码段。归根结底,您尝试将student*
分配给student
对象数组中的元素。指向对象的指针不同于对象:
stackArray[0] = new student(); // does NOT work
stackArray[0] = student();
请注意,该对象是由构造函数C++创建的,不一定涉及new
。当您使用 new
构造和对象时,您仍然会创建一个对象,但您也会在堆上为其分配空间。默认情况下,无论对象在何处定义,都会创建对象。例如,student()
在堆栈上创建一个对象,然后将其分配给stackArray[0]
内存中的对象。
与您的问题没有直接关系,但请注意您的 main() 无法正常工作。声明一个包含 20 个studentstack
元素的数组:
studentstack s[20];
稍后你正在做:
s.push(/* ... */);
s.pop(/* ... */);
这没有多大意义。 s
不是studentstack
.这是一个studentstack
数组。C++中的数组没有成员函数。
相关文章:
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 'operator='已弃用:改用 QDir::setPath()
- 过载'operator new'如何导致无限循环?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 处理"no operator found"
- 如何编写 operator= 用于使用虚拟方法与非平凡成员的匿名联合
- 运算符重载:"operator+"必须采用零个或一个参数
- 使用 operator() 扩展 Eigen::EigenBase
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- "operator +="行为异常的定义
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- 无法与'operator !='匹敌
- 无法与'operator='匹敌
- C++'operator='无法匹敌
- 在'* __result = * __first'中无法与'operator='匹敌
- QVector 无法与'operator='匹敌
- STL C++:无法与'operator*'匹敌
- 无法与'operator=='匹敌