C2597非法引用非静态成员

时间:2016-04-27 20:29:33

标签: c++ inheritance

试图弄清楚这个功课,我只是一个接一个地撞墙。我现在得到的是错误信息:

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

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

circle.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;

}

1 个答案:

答案 0 :(得分:1)

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

在您的情况下,静态函数setRadius正在尝试修改静态的成员变量radius。我怀疑你真的希望setRadius成为一个静态函数。