I wrote below code but getting error message
call of overloaded 'Shape()' is ambiguous
As I can see there is a no argument constructor, then why I am getting ambiguity error message for Shape() constructor, and how it can be resolved? I can see its working for object s2
and s3
but not for no argument constructor.
When I am declaring s1
as follows:
Shape s1();
// instead of Shape s1
Then I am getting following message for print
method of Shape
. Why? and what is meant by non-class type
?
request for member 'print' in 's1', which is of non-class type 'Shape()'
class Shape {
int j;
public:
int i;
const char* const c;
double print() const {
printf("Value of i is %d, j is %d and c is %s\n", i, j, c);
return 0;
}
Shape() :
i(10), j(0), c("Some string") {
}
Shape(int i = 10) :
i(10), j(10), c("Some String") {
this->i = i;
}
Shape(char* c) :
i(), j(), c(strcpy(new char[strlen(c)], c)) {
}
};
int main() {
Shape s1; // call of overloaded 'Shape()' is ambiguous
Shape* s2 = new Shape(1);
Shape s3("Some string");
s1.print(); // When using Shape s1() I am getting request for
// member 'print' in 's1', which is of non-class type 'Shape()'
return 0;
}