PDA

View Full Version : Need some help please


bmonday87
09-29-2009, 01:28 PM
im having trouble getting my program to work, i have sorting through errors for hours and am completely stumped, and odds are its going to be something really stupid. thanks in advance

Errors

animshapeO.h:28: error: cannot declare parameter '<anonymous>' to be of abstract type 'shapeO'
shapeO.h:3: note: because the following virtual functions are pure within 'shapeO':
shapeO.h:9: note: virtual bool shapeO::collidesWith(shapeO*)
p2.cpp: In function 'int main()':
p2.cpp:49: error: cannot allocate an object of abstract type 'animshapeO'
animshapeO.h:7: note: because the following virtual functions are pure within 'animshapeO':
shapeO.h:9: note: virtual bool shapeO::collidesWith(shapeO*)
p2.cpp:15: warning: unused variable 'w'
p2.cpp:15: warning: unused variable 'h'
animshapeO.h:28: error: cannot declare parameter '<anonymous>' to be of abstract type 'shapeO'
shapeO.h:3: note: because the following virtual functions are pure within 'shapeO':
shapeO.h:9: note: virtual bool shapeO::collidesWith(shapeO*)
animshapeO.cpp:21: error: cannot declare parameter 'shape' to be of abstract type 'shapeO'
shapeO.h:3: note: since type 'shapeO' has pure virtual functions




animshape.cpp

#include "animshapeO.h"
#include "shapeO.h"
#include <iostream>
#include <stdlib.h>


void animshapeO::update(){

x = x + movex;
y = y + movey;

}
void animshapeO::setDirectionVector(){

movex = -5 + drand48() * 10;
movey = -5 + drand48() * 10;


}

bool animshapeO::collidesWith(shapeO shape)
{

double rx,ry,rwidth,rheight;

shape.getRect(rx,ry,rwidth,rheight);

if (x+width > rx && x < rx+rwidth && x+height > rx && x < rx+ rheight)
return true;
else
return false;


return false;
}

-----------------------------------------------------------------------
animshapo.h

#ifndef animshapeO_H
#define animshapeO_H
#include "shapeO.h"


class animshapeO : public shapeO
{
private:
double x,y;
double movex, movey;


public:
animshapeO() : shapeO()
{

}
animshapeO(double X , double Y, double W, double H):shapeO()
{
shapeO::setRect(X,Y,W,H);

}

void update();

void setDirectionVector();

bool collidesWith(shapeO);
};
#endif

------------------------------------------------------------------------
shapeO.h

#ifndef SHAPEO_H
#define SHAPEO_H
class shapeO {
public:

shapeO() {};
virtual ~shapeO() {};

virtual bool collidesWith( shapeO *) = 0 ;
void getRect(double & X, double & Y, double & W, double & H)
{ X = x; Y = y; W = width; H = height; }
void setRect(double X , double Y, double W, double H)
{ x = X; y = Y; width=W; height=H;}
protected:
double x,y, height, width;
};
#endif

Reedbeta
09-29-2009, 02:46 PM
BTW, you can use the ... tags to post code in the forum.

Anyway, your problem is that the collidesWith function is trying to take a shapeO passed by value, but you actually need it to be passed by reference. So, the collidesWith function should take either a pointer (shapeO *) or a reference (shapeO &). This needs to be updated in both shapeO and animshapeO.

BTW, perhaps the indentation got messed up in the paste to the forum, but if not, you should make an effort to indent/format your code more cleanly...it makes it much easier to read. ;)

bmonday87
09-29-2009, 03:47 PM
Thanks alot

Reedbeta
09-29-2009, 03:55 PM
You shouldn't re-declare the variables in animshapeO. They already exist since you derived from shapeO. You can access them by name just like any other member variable; no special syntax needed.