为了弄清楚这个作业,我只是不断地碰壁。我现在得到的是错误消息:

错误1错误C2597:非法引用非静态成员'circleType :: radius'

我有2个头文件,circleType.h和cylinderType.h,我需要输出用户输入的运输和绘画费用的结果。在我完全失去主意之前需要一点帮助...谢谢。

圆h

class circleType
{
public:
static void setRadius(double r);

double getRadius();
double area();
double circumference();

circleType(double r = 0);

private:
double radius;

};

void circleType::setRadius(double r)
{
if (r >= 0)
{
    radius = r;
}
else
{
    radius = 0;
}
}

double circleType::getRadius()
{
return radius;
}

double circleType::area()
{
return 3.1416 * radius * radius;
}

double circleType::circumference()
{
return 2 * 3.1416 * radius;
}

circleType::circleType(double r)
{
setRadius(r);
}


cylinderTyper.h

#include "circleType.h"

class cylinderType: public circleType
{
public:
static void setRadius(double r);

static double getRadius();
static double area();
static double circumference();

cylinderType(double r = 0);

private:

double radius;

};


main.cpp

#include "stdafx.h"
#include "cylinderType.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void enterData(int& cylinderBase,int& cylinerHeight, double& shipCost,     double& paintCost);

int main()
{
cout << fixed << showpoint << setprecision(2);

int cylinderBase, cylinderHeight;
double sCost, pCost, shipCost, paintCost, volume, area = 0, circumference = 0;

enterData(cylinderBase, cylinderHeight, shipCost, paintCost);

cylinderType::setRadius(cylinderBase + cylinderHeight);
cylinderType::getRadius();
cylinderType::area();
cylinderType::circumference();

cout << "Cost of shipping: $" << circumference * shipCost << endl;

cout << "Cost of painting: $" << area * paintCost << endl;

system("pause");
return 0;
}

void enterData(int& cylinderBase, int& cylinderHeight, double& shipCost, double& paintCost)
{
cout << "Enter the base size of cylinder: ";
cin >> cylinderBase;
cout << endl;

cout << "Enter the hight size of cylinder: ";
cin >> cylinderHeight;
cout << endl;

cout << "Enter shipping cost per liter: ";
cin >> shipCost;
cout << endl;

cout << "Enter cost of painting per square foot: ";
cin >> paintCost;
cout << endl;

}

最佳答案

这是一个非常简单的规则:静态成员函数也只能访问静态成员变量。那是因为未针对特定对象调用静态函数,因此对象成员在该上下文中没有意义。

在您的情况下,静态函数setRadius试图修改非静态的成员变量radius。我怀疑您真的不希望setRadius是静态函数。

关于c++ - C2597非法引用非静态成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36900254/

10-17 01:32