[英]Member reference base type 'double' is not a structure or union
I'm new to C++ and can't figure out how to fix the error, would greatly appreciate for your help The part where the error occurs I'm trying to input the radius into the cirArea[]
array, but it does not seem to work. 我是C ++的新手,无法解决错误,将非常感谢您的帮助。发生错误的部分是我试图将半径输入
cirArea[]
数组,但似乎没有去工作。
Here's a part of my code: 这是我的代码的一部分:
int main(){
Circle *area;
double cirRadius;
int numCircle;
cout << "How many circles?" << endl;
cin >> numCircle;
double cirArea[numCircle];
for (int i = 0; i < numCircle; i++){
cout << "Enter the radius: ";
cin >> cirRadius;
cirArea[i].setRadius(cirRadius);
}
}
For setRadius(): 对于setRadius():
void Circle::setRadius(double r){
if (r >= 0)
radius = r;
else {
cout << "Invalid radius\n";
exit(EXIT_FAILURE);
}
}
so here's the ERROR: 所以这是错误:
member reference base type 'double' is not a structure or union
cirArea[i].setRadius(cirRadius);
~~~~~~~~~~^~~~~~~~~~
You cannot create an statically allocated array with 'not a constant size'. 您不能使用“非恒定大小”创建静态分配的数组。 Second issue is that
cirArea
is not of Circle
type but of double
. 第二个问题是
cirArea
不是Circle
类型,而是double
类型。
You would have to allocate it dynamically (and delete it afterwards) and make it Circle
type: 您将不得不动态分配它(然后将其删除),并使其成为
Circle
类型:
int main(){
double cirRadius;
int numCircle;
cout << "How many circles?" << endl;
cin >> numCircle;
Circle *area = new Circle[numCircle];
for (int i = 0; i < numCircle; i++){
cout << "Enter the radius: ";
cin >> cirRadius;
cirArea[i].setRadius(cirRadius);
}
delete[] area;
}
But a preferable way of doing it would be to use a std::vector
但是,更可取的方法是使用
std::vector
#include <iostream>
#include <cstdlib>
#include <vector>
struct Circle
{
double radius;
void setRadius(double r){
if (r >= 0)
radius = r;
else {
std::cout << "Invalid radius\n";
exit(EXIT_FAILURE);
}
}
} ;
int main(){
double cirRadius;
int numCircle;
std::cout << "How many circles?\n";
std::cin >> numCircle;
std::vector<Circle> area;
area.reserve(numCircle);
for (int i = 0; i < numCircle; ++i){
std::cout << "Enter the radius: ";
std::cin >> cirRadius;
area.emplace_back();
area.back().setRadius(cirRadius);
}
for( Circle& i : area )
{
std::cout << i.radius << '\n';
}
}
Your cirArea
has a type double, not Circle
, so you cant't apply on it Circle
method. 您的
cirArea
具有double类型,而不是Circle
类型,因此无法在其上应用Circle
方法。 Change the type of cirArea
from double
to Circule
to fix the error. 将
cirArea
的类型从double
更改为Circule
以修复错误。
It's essentially this statement that's at fault: 基本上这是错误的陈述:
double cirArea[numCircle];
Variable length arrays are not supported in C++, and I think you mean to use a Circle
object as the element rather than a double
. C ++不支持可变长度数组,我想您的意思是使用
Circle
对象作为元素而不是double
。
Use a std::vector<Circle>
instead to model a vectors of Circle
objects. 使用
std::vector<Circle>
代替对Circle
对象的向量建模。
The problem is that cirArea
is an array of double
and not of Circle
. 问题是
cirArea
是double
的数组,而不是Circle
。 The double
type does of course not have Circle
member functions (or any member functions at all). 当然,
double
类型没有Circle
成员函数(或根本没有任何成员函数)。
Since your compiler seems to support variable-lenght arrays I suppose you really want 由于您的编译器似乎支持可变长度数组,所以我想您真的想要
Circle cirArea[numCircle];
Variable-length arrays are not a standard C++ feature, if you want to make the program portable you should use std::vector
like 可变长度数组不是标准的C ++功能,如果您想使程序可移植,则应使用
std::vector
std::vector<Circle> cirArea(numCircle);
double cirArea[numCircle];
is an array
of double
. 是
double
的array
。 It should be an array
of Circle
. 它应该是
Circle
的array
。 However, numCircle
is a non-const so you can not do that (even if the compiler allow it. It is not standared). 但是,
numCircle
是一个非常量,因此您不能这样做(即使编译器允许它也是如此。它不是标准的)。 You should use dynamically allocated array
or even better a std::vector
. 您应该使用
dynamically allocated array
,甚至最好使用std::vector
。
A complete C++ solution would be : 完整的C ++解决方案是:
int main(){
Circle *area;
double cirRadius;
int numCircle;
cout << "How many circles?" << endl;
cin >> numCircle;
std::vector<Circle> cirArea;
cirArea.reserve(numCircle);
for (int i = 0; i < numCircle; i++){
cout << "Enter the radius: ";
cin >> cirRadius;
cirArea.emplace_back();
cirArea.back().setRadius(cirRadius);
}
}
if Circle
accept Radius
as constructor argument
, you may replace those two lines: 如果
Circle
接受Radius
作为constructor argument
,则可以替换这两行:
cirArea.emplace_back();
cirArea.back().setRadius(cirRadius);
with: 与:
cirArea.emplace_back(cirRadius);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.