错误:在 C++ 中无效使用非静态成员函数

烟熏范登伯格

这是我写的代码我想有很多改进的空间......

我现在仍在学习我的主要重点是让它发挥作用

稍后我会尝试优化这里和那里的东西

#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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Linux-信号:错误:无效使用非静态成员函数

C ++中具有静态成员的类的编译错误

C ++错误:必须调用对非静态成员函数的引用

Arduino错误(C ++):无效使用非静态数据成员

C ++ 11:使用非静态成员函数作为类构造函数中的默认参数

无效使用非静态成员函数C ++

线程错误:无效使用非静态成员函数

C ++ freeRTOS任务,无效使用非静态成员函数

控制可能会在C ++中达到非无效函数错误的结尾

在C ++中是否继承了静态成员函数?如果是,为什么不会出现歧义错误?

错误:ISO C ++禁止使用不合格或带括号的非静态成员函数的地址形成指向成员函数的指针

将成员函数作为函数变量传递时,无效使用非静态成员函数C ++

将ino sketch转换为C ++类,无效使用非静态成员函数

使用C ++的Array中的非静态成员引用错误

学习C ++并遇到早期示例错误(非静态数据成员之前需要构造函数)

C ++使用模板类调用非静态成员函数

Eigen中的C ++静态数据成员初始化错误

如何修复C ++中的“错误:无效使用非静态数据成员'tree :: root'”错误?

函数错误C中的静态数组

模板朋友功能C ++无效使用非静态数据成员错误

C ++无效使用非静态数据成员

C ++中的静态成员函数错误

我在DLL项目C ++中收到“非静态成员引用必须相对于特定对象”错误

我使用C ++引用函数或数据成员吗?(必须调用对非静态成员函数的引用)

错误:无效使用非静态成员

非静态成员函数c++线程linux的无效使用

错误:非静态成员函数 C++ 的无效使用

c ++ 17 错误消息:在向量上使用宏时“必须调用对非静态成员函数的引用”

C++ [错误] 无效使用非静态数据成员 'Tree::root'