Polymorphism (my post is to the point)

So I'm trying to create a Circle class that extends Shape class which has three abstract classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 *  Shape.h
 */
#ifndef SHAPE_H
#define SHAPE_H

class Shape {
	public:
	virtual void area () = 0;
	virtual void perimeter () = 0;
	virtual void volume () = 0;	
};

#endif 

I know circle's don't have volume, but bare with me. Anyway I've tried compiling my main program: ShapeTest.cpp, but I get the following error message:

ShapeTest.cpp:18: error: request for member ‘area’ in ‘x’, which is of non-class type ‘Circle ()()’
ShapeTest.cpp:19: error: request for member ‘perimeter’ in ‘x’, which is of non-class type ‘Circle ()()’
ShapeTest.cpp:20: error: request for member ‘volume’ in ‘x’, which is of non-class type ‘Circle ()()’

What am I doing wrong? (I think the problem is either in Circle.h or Circle.cpp)

Here is Circle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CIRCLE_H
#define CIRCLE_H

#include "Shape.h"

class Circle : public Shape
{
	public:
	void area ();
	void perimeter ();
	void volume ();
	Circle ();
	
	protected:
	double pi;
	double r;
};

#endif 

Here is Circle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Circle.h"
#include <iostream>
using namespace std;

void Circle::area()
{
	cout << "area equals " << pi * r * r << "." << endl;
}

void Circle::perimeter ()
{
	cout << "The circumference of the circle is " << 2*pi*r << "." << endl;
}

void Circle::volume ()
{
	cout << "error: Circle is a 2-D object." << endl;
	cout << "Two dimensional objects do not have volume!" << endl;
}

Circle::Circle ()
{
	pi = 3.14;
	r = 1;
}

Last edited on
The problem is actually in ShapeTest.cpp, on lines 18-20. This is hinted at by the compiler error:

 
ShapeTest.cpp:18: error: request for member ‘area’ in ‘x’, which is of non-class type ‘Circle ()()’


You are probably declaring 'x' wrong or something like that. Can you post your ShapeTest.cpp code?
Sure. Thank you for responding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 *  ShapeTest.cpp
 *  
 *
 *  Created by Matthew Dunson on 11/30/09.
 *  Copyright 2009 The Ohio State University. All rights reserved.
 *
 */


#include "Circle.h"
#include <iostream>
using namespace std;

int main ()
{
	Circle x ();
	x.area ();
	x.perimeter ();
	x.volume ();
}

Your are not making a Circle on line 17, instead, you are prototyping a function that returns a Circle and is called x. Since you want the default constructor, just remove the () after the x.
OOPS! I guess I shouldn't put the parenthesis if it is a default constructor. I think I fixed it.

Here is my output:

The circumference of the circle is 6.28.
error: Circle is a 2-D object.
Two dimensional objects do not have volume!


Sometimes i blend C++ code with Java code.
Theoretically, it should work in C++ too, but apparently that's just how it works. :/
You guys are awesome. This is the best place on the web to get a live person to help you with your code if you are learning c++.
The problem is that the syntax for defining a function prototype called x, taking no parameters and returning a Circle is exactly the same as declaring a Circle instantiation called x with the parameterless constructor. C++0x is going to fix this, I think, by allowing you to use {} or something for the parentheses of a constructor.
Topic archived. No new replies allowed.