試圖讓這個家庭作業想通了,我只是繼續打一個接一個的牆。我現在我得到的是錯誤信息:C2597對非靜態會員的非法引用
錯誤1錯誤C2597:非法引用非靜態成員「circleType ::半徑」
我有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;
}
您在基類和派生類中都有半徑:我懷疑這是問題 – rts1
「setRadius」方法不應該是靜態的。 –
根本不應該有'cylinderType :: setRadius'函數,因爲正確的版本已經從基類繼承。氣缸只需執行高度處理,並計算所有曲面的總面積(兩個面等於曲面面積,一個曲面可能與圓周有關)。 –