error C2059 doing my head in

This is how the code is in my book, but it's stil throwing out errors. I think I know why but I cannot put my finger on it, spent 2 hours trying to figure it out on my own but I just can't figure it out.

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
26
27
28
29
30
31
#include <iostream>
using namespace std;

class Shape
{
public:
	virtual int getArea() = 0;
	virtual int getEdge() = 0;
	virtual void Draw() = 0;
};

class Rect : public Shape
{
private:
	int height, width;
public:
	Rect( int initWidth, int initHeight)
	{
		height = initHeight;
		width = initWidth;
}
	~Rect();

	int getArea() { return height * width ) ; //Last ) says expects ;
	int getEdge() {  return ( 2 * height ) + ( 2 * width ) ; } //First { says expects ;


int main()
{
	
}//Expects  } 


There is also an "Expected a } at the second } in the main body, could anyone give me any ideas on what's wrong?

EDIT
int getArea() { return ( height * width ) ;
Fixed the first error so far
Last edited on
int getArea() { return height * width ) ; should be int getArea() { return height * width; }

also you need a closing brace and ; at the end of your class, and your destructor should have a body even if its empty.
Last edited on
Thanks! By the destructor (sorry still learning) you mean ~Rect(); right? If I put a body i.e
{

}

I get an error saying expected a declartion.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

class Shape
{
public:
	virtual int getArea() = 0;
	virtual int getEdge() = 0;
	virtual void Draw() = 0;
};

class Rect : public Shape
{
private:
	int height, width;
public:
	Rect( int initWidth, int initHeight)
	{
		height = initHeight;
		width = initWidth;
};
	~Rect();

	int getArea() { return height * width ; }
	int getEdge() { return ( 2 * height ) + ( 2 * width ) ; }

	void Drawn()
	{
		for ( int i = 0 ; i < height ; i++ ) {
			for ( int j = 0 ; j < width ; j++ ) { cout << "x " ; }
			cout << endl ; }

		}

	};



int main()
{
	Shape* pQuad = new Rect( 3,7);// Error object of abstract class type "Rect" is not //allowed. Pure virtual function "Shape::Draw" has no overrider
	Shape* pSquare = new Rect( 5,5);

	pQuad -> Draw();
	cout << "Area is " << pQuad -> getArea() << endl;
	cout << "Perimter is " << pQuad -> getEdge() << endl;

	pSquare -> Draw();
	cout << "Area is " << pSquare -> getArea() << endl ;
	cout << "Perimter is " << pSquare -> getEdge() << endl;
}


Errors are:
1> Source.cpp
1>c:\users\Admin\documents\visual studio 2012\projects\adt\adt\source.cpp(41): error C2259: 'Rect' : cannot instantiate abstract class
1> due to following members:
1> 'void Shape::Draw(void)' : is abstract
1> c:\users\Admin\documents\visual studio 2012\projects\adt\adt\source.cpp(9) : see declaration of 'Shape::Draw'
1>c:\users\Admin\documents\visual studio 2012\projects\adt\adt\source.cpp(42): error C2259: 'Rect' : cannot instantiate abstract class
1> due to following members:
1> 'void Shape::Draw(void)' : is abstract
1> c:\users\Admin\documents\visual studio 2012\projects\adt\adt\source.cpp(9) : see declaration of 'Shape::Draw'

I understand the error, but not how to fix it if that makes much sense?
Last edited on
remove the ;
1
2
3
4
5
	~Rect();

	{

	}


should be
1
2
3
4
5
~Rect()

	{

	}
Last edited on
Dam book, thought something was off, now just gotta beat this C2259 error ^^ If anyone else is kind enough please spare some time to help a noob (only been learning a month) out :)
Last edited on
"Draw" is not the same as "Drawn"
MY GOD How did I not see that, thank you very much!

Isn't drawing how it's suppose to
http://imgur.com/keg8kWa

Top one is the one I'm struggling with, the bottom one is fine. So it must something linked to that....

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

class Shape
{
public:
	virtual int getArea() = 0;
	virtual int getEdge() = 0;
	virtual void Draw() = 0;
};

class Rect : public Shape
{
private:
	int height, width;
public:
	Rect( int initWidth, int initHeight)
	{
		height = initHeight;
		width = initWidth;
};
	~Rect()
	{

	}


	int getArea() { return height * width ; }
	int getEdge() { return ( 2 * height ) + ( 2 * width ) ; }

	void Draw()
	{
		for ( int i = 0 ; i < height ; i++ ) {
			for ( int j = 0 ; j < width ; j++ ) { cout << "x " ; }
		cout << endl ; }

		}

	};

int main()
{
	Shape* pQuad = new Rect(3,7);
	Shape* pSquare = new Rect(5,5);

	pQuad -> Draw();
	cout << "Area is " << pQuad -> getArea() << endl;
	cout << "Perimter is " << pQuad -> getEdge() << endl;

	pSquare -> Draw();
	cout << "Area is " << pSquare -> getArea() << endl ;
	cout << "Perimter is " << pSquare -> getEdge() << endl;
}
Last edited on
Could you be more specific than "it is not doing what it should be doing"? What is it supposed to be doing?
Ah my apolgozies, the * on top are suppose to be 7 characters wide from left to right. But for some reason it's not doing so, perhaps theres a small typo in the code or it's just me on my end outputting it differents to others compiling. My IDE is Visual Studio.
Last edited on
The first parameter is the width, second the height, so if the rectangle should be 7 characters wide, did you mean to send 7,3 instead of 3,7?

1
2
3
Rect( int initWidth, int initHeight)
///
Shape* pQuad = new Rect(3,7);
Typo in the book, I should've known but when it's written in a book you tend to shift the blame on yourself. Thanks mate, I stopped ages ago trying to learn C++ when I got to this bit.
Now I know to trust my instinicts if something looks wrong. Thanks once again :)
Topic archived. No new replies allowed.