The reference number of the base type "double" is not a structure or union

I am new to C ++ and I can’t figure out how to fix the error, I really appreciate your help. Part of where the error occurs, I try to enter the radius into the cirArea[] array, but it does not work.

Here is 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 ():

 void Circle::setRadius(double r){ if (r >= 0) radius = r; else { cout << "Invalid radius\n"; exit(EXIT_FAILURE); } } 

so here is ERROR:

 member reference base type 'double' is not a structure or union cirArea[i].setRadius(cirRadius); ~~~~~~~~~~^~~~~~~~~~ 
+5
source share
5 answers

You cannot create a statically allocated array with a "non-constant size". The second problem is that cirArea not of type Circle , but double .

You will have to select it dynamically (and delete it afterwards) and make it Circle type:

 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 the preferred way to do this would be to use 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'; } } 

http://cpp.sh/3l7ti

+2
source

Your cirArea is of type double, not Circle , so you cannot apply it to the Circle method. Change the cirArea type from double to Circule to fix the error.

+1
source

In fact, this is a statement that the fault:

double cirArea[numCircle];

Variable-length arrays are not supported in C ++, and I think you want to use the Circle object as an element, not a double .

Use std::vector<Circle> instead to model the vectors of Circle objects.

+1
source

The problem is that cirArea is a double array, not a Circle . The double type, of course, does not have Circle member functions (or any member functions in general).

Since your compiler seems to support variable length arrays I suppose you really want

 Circle cirArea[numCircle]; 

Variable-length masks are not a standard C ++ function, if you want to make the program portable, you should use std::vector as

 std::vector<Circle> cirArea(numCircle); 
+1
source

double cirArea[numCircle]; is an array of double . It should be an array of Circle . However, numCircle is non-constant, so you cannot do this (even if the compiler allows it. It is not independent). You should use a dynamically allocated array or even better std::vector .

A complete solution in C ++ would be:

 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 a constructor argument , you can replace these two lines:

 cirArea.emplace_back(); cirArea.back().setRadius(cirRadius); 

with:

 cirArea.emplace_back(cirRadius); 
0
source

Source: https://habr.com/ru/post/1245533/


All Articles