这是我写的代码我想有很多改进的空间......
我现在仍在学习我的主要重点是让它发挥作用
稍后我会尝试优化这里和那里的东西
#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;
};
我得到的错误仅在 cout 行中
事情对我来说有点不清楚,因为我目前在旅行时使用 mac 进行编码……我以前从未使用过苹果笔记本电脑
我读过一些文章说某些东西应该设置为静态
您缺少函数调用所需的括号
std::cout << "Name " << MovieOne.accessName()
<< " MPAA " << MovieOne.accessMPAA()
<< " AVG Rating is " << MovieOne.getAverage()
<< '\n';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句