【问题标题】:Errors while compiling while I read an array of objects读取对象数组时编译时出错
【发布时间】:2020-07-15 12:33:17
【问题描述】:
#include <iostream>
#include <cstring>
using namespace std;

class Film {
private:
    string name;
    int year_prod;
    string producer;
    string main_actor;

public:
    Film();
    void print()
    {
        cout << "\nThe name of movie: " << name;
        cout << "\nThe year of produced: " << year_prod;
        cout << "\nProducer: " << producer;
        cout << "\nMain actor: " << main_actor << endl;
    }
    void SetName(string xName)
    {
       name = xName;
    }

    string GetName()
    {
       return name;
    }

    void SetYearP(int xYearP)
    {
        year_prod = xYearP;
    }

    int GetYearP()
    {
        return year_prod;
    }

    void SetProducer(string xProducer)
    {
        producer = xProducer;
    }

    string GetProducer()
    {
        return producer;
    }

    void SetMaina(string xMaina)
    {
        main_actor = xMaina;
    }

    string GetMaina()
    {
        return main_actor;
    }


};


int main()
{
    Film obs[100]; // maximum of 100 hundred films
    int n;
    cout << "how many films ";
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        string name;
        int year;
        string prod;
        string actor;
        cout << "enter the film name ";
        cin >> name;
        cout << "enter the production year ";
        cin >> year;
        cout << "enter the producer name ";
        cin >> prod;
        cout << "enter the actor name ";
        cin >> actor;
        obs[i].SetName(name);
        obs[i].SetYearP(year);
        obs[i].SetProducer(prod);
        obs[i].SetMaina(actor);
    }
} 

我已经完成了一半的代码,但在编译时出现错误提示:函数 _main AND 1 中引用的未解析的外部符号“public: __thiscall Film::Film(void)”(??0Film@@QAE@XZ)未解决的外部。我不确定我是否以正确的方式从用户输入中获得了 n Film 的对象,因为我仍然是 OOP 的初学者。

【问题讨论】:

标签: c++ function class oop constructor


【解决方案1】:

你没有实现你的构造函数:

public:
    Film();//<-- declared, but not defined.

如果你不希望你的构造函数做任何事情,只需添加一个空的主体:

public:
    Film() {};

或者更好的是,明确地将其声明为默认构造函数:

public:
    Film()=default;

【讨论】:

    【解决方案2】:

    请试试这个。希望这会有所帮助。

    class Film {
    ...
    ...
    ...
    public:
            //Film(); Remove this line if you are not defining Film()
            void print()
            {
                cout << "\nThe name of movie: " << name;
                cout << "\nThe year of produced: " << year_prod;
                cout << "\nProducer: " << producer;
                cout << "\nMain actor: " << main_actor << endl;
            }
    ...
    ...
    ...
    }
    

    如果您希望它成为默认构造函数,请添加一个空块 Film(){}

    【讨论】:

      【解决方案3】:

      你必须为你的类实现构造函数。构造函数是创建类实例的方法。 set函数的目的是修改对象的属性,但属性的初始化必须在构造函数中完成。

      例如:

      public:
        //default constructor: it does not take 
        //anything as input, it sets name as an 
        //empty string and the year to 1900
        Film(){
          name = "";
          year = 1900;
        }
        // It creates a Film object, whose name 
        // is NAME and whose year is YEAR
        Film(string NAME, int YEAR){
          name = NAME;
          year = YEAR;
        }
      
      // set function which allows to modify the 
      // year of a Film object.
      void setYear(int newYear){
        year = newYear;
      }
      

      查看here 了解快速介绍。

      编辑:您可能希望将默认构造函数设置为

      Film(){};
      

      这样你必须调用所有的集合函数来初始化它的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2016-04-29
        • 2018-08-01
        • 2020-06-28
        相关资源
        最近更新 更多