这是我写的代码 我想还有很多改进的空间...
我现在仍在学习我的主要重点只是如何使其发挥作用
稍后尝试在这里和那里进行优化
#include <iostream>
using namespace std;
class Movie
{
private:
std::string Name, MPAA;
public:
int Terrible = 0, Bad = 0, Ok = 0, Good = 0, Great = 0, TotalRated = 0;
Movie()
{
Name = "";
MPAA = "";
TotalRated = 0;
}
void addRating (int i); // decleration of rating function
double getAverage(void); // decleration of avg function
std::string accessName() {return(Name);} // accessor for Name
void mutateName(std::string aName);
std::string accessMPAA() { return MPAA;} // accessor for MPAA
void mutateMPAA(std::string aMPAA);
};
void Movie::mutateName(std::string aName) // mutator for Name
{
Name = aName;
}
void Movie::mutateMPAA(std::string aMPAA) // mutator for MPAA
{
MPAA = aMPAA;
}
void Movie::addRating(int i) // not the most elligant but it does the job, checks and adds rating. might try using an array
{
if((i > 0) && (i < 6))
{
if(i == 1)
{
Terrible++;
TotalRated++;
}
if(i == 2)
{
Bad++;
TotalRated++;
}
if(i == 3)
{
Ok++;
TotalRated++;
}
if(i == 4)
{
Good++;
TotalRated++;
}
if(i == 5)
{
Great++;
TotalRated++;
}
}
else
{
std::cout << "your input was Invalid" << endl; // outputs error for invalid submition
}
};
double Movie::getAverage() // gets avg rating (total score / total times rated)
{
return (Terrible + (Bad * 2) + (Ok * 3) + (Good * 4) + (Great * 5)) / TotalRated;
};
int main()
{
Movie MovieOne;
//Movie 1
MovieOne.mutateName("Action");
MovieOne.mutateMPAA("PG13");
//Viewer 1
MovieOne.addRating(5);
//Viewer 2
MovieOne.addRating(3);
//Viewer 3
MovieOne.addRating(3);
//Viewer 4
MovieOne.addRating(2);
//Viewer 5
MovieOne.addRating(4);
std::cout << "Name " << MovieOne.accessName << " MPAA " << MovieOne.accessMPAA << " AVG Rating is " << MovieOne.getAverage << endl;
};
我得到的错误仅在提示行中
对于我来说,事情有点不清楚,因为我目前正在旅行时使用Mac进行编码。.我以前从未使用过苹果笔记本电脑
我读过一些文章,说应该将某些内容设置为静态
答案 0 :(得分:1)
您缺少函数调用所需的括号
std::cout << "Name " << MovieOne.accessName()
<< " MPAA " << MovieOne.accessMPAA()
<< " AVG Rating is " << MovieOne.getAverage()
<< '\n';