';周长';未在此范围中声明 我正在用C++语言进行入门课程。我一直在犯这个错误。代码正在使用头文件,我不知道如何有效地将用户数字输入从.cpp文件传输到.h文件,反之亦然。以下是我的.cpp文件代码: #include<iostream> //ignore this part the stack overflow code editor //is being weird here #include<string> #include "Circle.h" using namespace std; int main() { //instantiate the Circle.h code Circle ObjData; //object to store information in //user inputs radius value cout << "Please enter the initial radius: "; cin >> ObjData.getRadiusVal(); //display the circle radius check cout << "\nThis is the radius of the circle: " << RadiusVal << endl; //display the rest of the circle calculations //get the calculated variables from circle.h getCircleDiam() const; { return CircleDiam } getCircumference() const; { return Circumference } getCircleArea() const; { return CircleArea } cout << "This is the diameter of the circle: " << ObjData.getCircleDiam() << endl; cout << "This is the circumference of the circle: " << ObjData.getCircumference() << endl; cout << "This is the area of the circle: " << ObjData.getCircleArea() << endl; } #包括 //忽略堆栈溢出代码编辑器中的这一部分 //这里很奇怪吗 #包括 #包括“Circle.h” 使用名称空间std; int main() { //实例化Circle.h代码 圈出ObjData;//要在其中存储信息的对象 //用户输入半径值 cout>ObjData.getRadiusVal(); //显示圆半径检查 cout
您应该关注以下实现。您的头文件应该类似于以下内容:';周长';未在此范围中声明 我正在用C++语言进行入门课程。我一直在犯这个错误。代码正在使用头文件,我不知道如何有效地将用户数字输入从.cpp文件传输到.h文件,反之亦然。以下是我的.cpp文件代码: #include<iostream> //ignore this part the stack overflow code editor //is being weird here #include<string> #include "Circle.h" using namespace std; int main() { //instantiate the Circle.h code Circle ObjData; //object to store information in //user inputs radius value cout << "Please enter the initial radius: "; cin >> ObjData.getRadiusVal(); //display the circle radius check cout << "\nThis is the radius of the circle: " << RadiusVal << endl; //display the rest of the circle calculations //get the calculated variables from circle.h getCircleDiam() const; { return CircleDiam } getCircumference() const; { return Circumference } getCircleArea() const; { return CircleArea } cout << "This is the diameter of the circle: " << ObjData.getCircleDiam() << endl; cout << "This is the circumference of the circle: " << ObjData.getCircumference() << endl; cout << "This is the area of the circle: " << ObjData.getCircleArea() << endl; } #包括 //忽略堆栈溢出代码编辑器中的这一部分 //这里很奇怪吗 #包括 #包括“Circle.h” 使用名称空间std; int main() { //实例化Circle.h代码 圈出ObjData;//要在其中存储信息的对象 //用户输入半径值 cout>ObjData.getRadiusVal(); //显示圆半径检查 cout,c++,c++14,C++,C++14,您应该关注以下实现。您的头文件应该类似于以下内容: #include <cmath> class Circle { double RadiusVal; double CircleArea; double Circumference; double CircleDiam; public: // Constructor // Both, the dafault constructor // and the one that tak
#include <cmath>
class Circle {
double RadiusVal;
double CircleArea;
double Circumference;
double CircleDiam;
public:
// Constructor
// Both, the dafault constructor
// and the one that takes a double.
Circle(const double &r = 1.0) :
RadiusVal(r),
CircleArea(3.14159*pow(r,2)),
Circumference(2*3.14159*r),
CircleDiam(r*2) {}
// Set functions
// You can still implement these
// But consider that now when you modify one value,
// Other values should also change accordingly
void setCircleDiam(double);
void setCircumference(double);
void setCircleArea(double);
//Get functions
double getRadiusVal() const {return RadiusVal; }
double getCircleDiam() const {return CircleDiam; }
double getCircumference() const {return Circumference; }
double getCircleArea() const {return CircleArea; }
};
例如:
请输入初始半径:1
这是圆的半径:1
半径:1
直径:2
周长:6.28318
面积:3.14159
注意构造函数的使用,与“setter”方法相反,
是用初始值填充对象的正确方法(直接填充到对象的数据成员中)。“Setter”方法应该在我们想要修改的时候使用实例化的对象,而不是逐渐用值填充对象。另外,
setCircleDiam
在你的圈中。h
实际上不是一个成员函数。它缺少圈::
这难道不需要有.h文件吗?@adastraintfinitum检查下面的答案。.h
不是绝对必要的ssary,但将圈作为一个单独的文件,以便您可以在其他任何地方重用它可能会更有益。这是一个完全混乱的问题--请看一些关于如何定义类成员函数的代码示例谢谢这非常有帮助!!我仍然需要使用用户输入运行一些测试,但我认为这已经解决了我遇到的问题!这也提供了很多信息。@adastraintfinitum是的!您可能需要检查用户的有效输入,例如,这样半径就不会是负数,也不会有类似的理智约束。@adastraintfinitum继续:)嘿,我还有一个简单的问题。我修改了.h输入,以接受用户定义的值:构造函数应该在初始化所有数据成员。它的正文可以为空。
#include <cmath>
class Circle {
double RadiusVal;
double CircleArea;
double Circumference;
double CircleDiam;
public:
// Constructor
// Both, the dafault constructor
// and the one that takes a double.
Circle(const double &r = 1.0) :
RadiusVal(r),
CircleArea(3.14159*pow(r,2)),
Circumference(2*3.14159*r),
CircleDiam(r*2) {}
// Set functions
// You can still implement these
// But consider that now when you modify one value,
// Other values should also change accordingly
void setCircleDiam(double);
void setCircumference(double);
void setCircleArea(double);
//Get functions
double getRadiusVal() const {return RadiusVal; }
double getCircleDiam() const {return CircleDiam; }
double getCircumference() const {return Circumference; }
double getCircleArea() const {return CircleArea; }
};
#include<iostream>
#include "Circle.h"
int main() {
double rad;
//user inputs radius value
std::cout << "Please enter the initial radius: ";
std::cin >> rad;
Circle c(rad); // all construction logic is internal to Circle class
//display the circle radius check
std::cout << "\nThis is the radius of the circle: " << c.getRadiusVal() << '\n';
//display the rest of the circle calculations
std::cout << "Radius: " << c.getRadiusVal() << '\n'
std::cout << "Diameter: " << c.getCircleDiam() << '\n'
std::cout << "Circumference: " << c.getCircumference() << '\n'
std::cout << "Area: " << c.getCircleArea() << '\n'
}