Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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

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.

You would have to allocate 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 a preferable way of doing it would be to use a 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

I know :) I was just editing this answer but I couldn't do it any faster. Please check my edits - that should be fine. – Patryk Mar 22, 2016 at 8:41

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.

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

The problem is that cirArea is an array of double and not of Circle. The double type does of course not have Circle member functions (or any member functions at all).

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

std::vector<Circle> cirArea(numCircle);

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

A complete C++ solution 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 constructor argument, you may replace those two lines:

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

with:

cirArea.emplace_back(cirRadius); 
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.