Constructors question

So i have this code

1
2
3
4
double data;
cout << "Input data" << endl;
cin >> data;
Area(data);


Area is a class with a constructor that takes a double as a parameter. However the code above doesn't work and I get an error "Redefinition of 'data' with a different type: 'Area' vs 'double'. When i simply replace Area(data) with Area(5), the code will run. It seems as if it wouldn't take my variable as a parameter.
Last edited on
Is "Area" a class? or a object? Becuase if its a class, thats not how you call the constructor. Please show us more code. oh, and put the code between code tags - http://www.cplusplus.com/articles/jEywvCM9/

Edit: Read what @MiiNiPaa said
Last edited on
This is called Most vexing parse: If something can be reinterpreted as declaration, it will be. As parentheses usually can be dropped, line 4 is interpreted as Area data;, which is variable declaration.

Even if would not work like that, this line is still meaningless: it creates unnamed temporary which is instantly destroyed.

Maybe you wanted something like Area some_area(data);
Area is a class, as I will show below in my header file.

1
2
3
4
class Area: public Shape{
public:
    Area(double);
};


This is the corresponding class file.
1
2
Area::Area(double radius){
    area(radius*radius, 3.14);


Thank your for the quick reply.
Thank you MiiNiPaa, that explains a lot. I changed my code according to your advice. Thank you very much
Topic archived. No new replies allowed.