c++ - 错误 C2679 : binary '=' : no operator found which takes a right-hand operand of type 'School *' (or there is no acceptable conversion)

标签 c++ arrays pointers constructor

我正在尝试学习如何使用数组/指针访问构造函数。 我知道如何访问类成员函数,但我一直卡在如何访问构造函数上。

这个程序: 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/

相关文章:

java - 订购从 HashSet 创建的数组的最有效方法

c - C语言中删除字符串中的字母

c++ - 在 C++ 中集成 swf

C++ 在 1D 数组中表示 3D 数组

javascript - 映射有条件改变的嵌套字段

c - 为什么我的节点的 String 成员在创建后会打印一个空行?

c - C 中的指针变量

c++ - 使用嵌套类的链表?

c++ - C2259 错误 (directx 11)

python - 从 python 数组创建元素明智的字典