【问题标题】:Int Main (using classes) not returning a calculated variable [closed]Int Main(使用类)不返回计算变量[关闭]
【发布时间】:2015-07-14 12:36:24
【问题描述】:

我们正在学习我的 C++ 课程中的课程,对于家庭作业,我们必须创建一个球体课程来计算表面积和体积。我正在玩音量函数,试图让它在计算后返回V 的值。我正在阅读一些关于谷歌的东西,return 语句结束了函数并返回你在它之后写的任何内容,但它并没有为我这样做。我在这里没看到什么??

#include <iostream>
#include <cmath>
#include <string>

using namespace std;


class sphere
{
    private:
        double r;

    public:
        double sarea();
        double volume();
        void Input();
        sphere();
        double GetRad();


};

double ReadPositiveDouble(string prompt)
{
    double r=0.0;
    double rad=0.0;

    cout<<prompt<<endl;
    cin>>r;
    if(r>=0.0)
    {
        rad=r;
    }
    else
    {
        cerr<<"Cannot set radius to negative.\n";
    }   
    return r;
}

void sphere::Input()
{
    double rad=0.0;

    rad=ReadPositiveDouble("Radius?");
}

double sphere::GetRad()
{
    double rad=0.0;

    return rad;
}

sphere::sphere()
{
    double radius=0.0;
}

double sphere::sarea()
{
    double rad=0.0;

    return 4*M_PI*pow(rad, 2.0);
}


double sphere::volume()
{
    double rad=0.0;
    double V=0.0;

    GetRad();
    V=(4*M_PI*pow(rad,3.0))/3;

    return V;
}



int main()
{

    double r=0.0; 


    sphere(red);
    red.Input();

    //red.sarea();
    red.volume();




    system("Pause");

    return 0;
}

【问题讨论】:

  • 您是否修复了这些错误消息?即“声明类范围后缺少分号”?挖这个到底有什么难的?
  • 哦,对不起,我是 C++ 新手。我把它放在“球体”之后但它不起作用,所以我解开了它,看看是否有人可以提供帮助。
  • @heyheythere 之所以会有反对票,是因为这类问题是一个非常容易被问到的初学者问题。因此,将错误复制/粘贴到谷歌并查看其他人是否有同样的问题应该相对容易。搜索声明后缺少分号后的前两个谷歌结果完全回答了您的要求......

标签: c++ class friend return-type


【解决方案1】:

这是一个简单的错字 - class 定义的末尾必须有一个分号。

class sphere
{
    ...
};
 ^ this is required

【讨论】:

  • 我们应该有一个编译器来传递编译器错误消息,它解释了这些编译器错误消息的含义。不,等等……
  • 谢谢格雷格!!我是新手! ://
  • @heyheythere:欢迎。请注意,编译器已经尝试告诉您出了什么问题,并显示消息“在声明类 sphere 后缺少分号”。编译器错误消息有时会产生误导,但这很简单!
  • 是的,我看到了,但我尝试像这样在“类领域”之后放置:class sphere;,我只是让它变得更糟,所以我决定在这里问。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 2013-11-26
  • 2022-01-21
  • 2018-11-12
  • 2015-08-12
相关资源
最近更新 更多