我正在尝试学习如何使用数组/指针访问构造函数。 我知道如何访问类成员函数,但我一直卡在如何访问构造函数上。
这个程序: 1.询问学校数量。 2.询问学校名称。 3.显示每个学校的名称。
#include<iostream>
#include<string>
using namespace std;
class School
{
public:
School(string name = "")
{ schoolname = name;}
void Display()
{ cout << "School name is " << schoolname;}
private:
string schoolname;
};
int main()
{
string sname;
int schoolNO;
School *myschool;
myschool[10];
cout << "Enter number of school : ";
cin >> schoolNO;
for (int i = 0; i < schoolNO; i++)
{
cout << "Enter school name : ";
cin >> sname;
myschool[i] = new School(sname);*//The error stated is in this line...*
myschool[i].Display();
}
}
最佳答案
问题是 mySchool
是一个对象数组,而不是指针数组,因此您不能使用 new
将指针分配给它的元素。
替换这个:
School *myschool;
myschool[10];
为此:
School* myschool[10];
现在您有了一个指针数组,new
将起作用。
关于c++ - 错误 C2679 : binary '=' : no operator found which takes a right-hand operand of type 'School *' (or there is no acceptable conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409529/