-1

对于作业,我必须创建一个包含电影名称、MPAA 评级、从 1 到 5 评级的人数、找到每个评级的累积值和平均值的电影类。

我主要在构造函数和类方面遇到问题。我试图让它接受一个字符串(我得到了那部分有点正确)。我在第 77 行和第 83 行遇到了一些错误。我也被卡住了,因为我不知道下一步应该采取什么步骤。我将不胜感激任何可能的帮助。

这是我到目前为止得到的:

    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    #include <string>
    // Required headers
    using namespace std;

    //Class movie starts
    class Movie {
    public:
    void SetMovieName(string moviename);
    // Function to set the name of the movie
    // moviename = movie; later on

    string GetMPAAR();
    //function to return the MPAA rating

    int amountofratingsof1() const;
    //function to return the number of people that rated the movie as a 1
    int amountofratingsof2() const;
    //function to return the number of people that rated the movie as a 2 
    int amountofratingsof3() const;
    //function to return the number of people that rated the movie as a 3
    int amountofratingsof4() const;
    //function to return the number of people that rated the movie as a 4 
    int amountofratingsof5() const;
    //function to return the number of people that rated the movie as a 5

    int average() const;
    //function to return the average value of all ratings

    std::string Movies(string moviename = "Gavecube: The Movie");
    //constructor to set the movie



    private:
    string Movie; //variable to store the name of the movie
    int peoplethatrated1; // variable to store the number of people that rated 1
    int peoplethatrated2; // variable to store the number of people that rated 2
    int peoplethatrated3; // variable to store the number of people that rated 3
    int peoplethatrated4; // variable to store the number of people that rated 4
    int peoplethatrated5; // variable to store the number of people that rated 5
};

//implementation file:

void Movie::SetMovieName(const string moviename) {
    //function below checks if it is a string or not
    if (!cin) {
        cout << "Not a valid input. Please restart." << endl;
    }
}

int Movie::amountofratingsof1()const {

}

int Movie::amountofratingsof2()const {

}
int Movie::amountofratingsof3()const {

}
int Movie::amountofratingsof4()const {

}
int Movie::amountofratingsof5()const {

}

//constructor
std::string Movie(string moviename) {
    SetMovieName(moviesname)
}


int main()
{
    Movie Movies("Hello"); 



    return 0;
}

谢谢你。

4

1 回答 1

0

对于构造函数,它们不像普通函数。他们没有返回类型,甚至没有void。在构造函数中,如果可以使用成员启动,则应该这样做。如果您进入该部分,是对构造函数的一个很好的解释。Constructors一般来说,它应该看起来像:

ClassName (parameter par1, ...) : mem1(par1), ...{}

mem1对应的数据成员在哪里par1链接的更下方是介绍如何执行此操作的成员启动部分。一旦你设置了构造函数,你应该按照你想要的任何顺序完成你的成员函数定义,然后像任何其他程序一样进行测试和调试。如果您需要进行错误检查,请测试边缘情况和无效数字。

于 2018-03-10T05:26:05.447 回答