0
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

//the volume and surface area of sphere

struct sphere_t { float radius; }; 
sphere_t sph; 
sph.radius = 3; // this is the part that mess me up, error in sph.radius

double SphereSurfaceArea(const sphere_t &sph)
{
    return 4*M_PI*sph.radius*sph.radius; 
}

double SphereVolume(const sphere_t &sph)
{
    return 4.0/3.0*M_PI*sph.radius*sph.radius*sph.radius;
}

int main()
{

    cout << "Sphere's description: " << sph.radius << endl;
    cout << "Surface Area: " << SphereSurfaceArea(sph) << endl; 
    cout << "Volume :" <<SphereVolume(sph) << endl;

    system("pause");
    return(0);
}

私が得る出力は次のとおりです。

立体の説明 表面積 体積

定数参照によって const 関数に数値を入れ、何も返さずに関数 void を設定するにはどうすればよいですか?

4

2 に答える 2

2

初期化をグローバル変数の定義と一緒に 1 行にまとめることができます。

sphere_t sph = { 3 }; 
于 2013-04-03T22:07:08.970 に答える