Question

Ok, I'm having this problem with my Sprite class. Basically the sprite class should have a object of class Vector as its member with Vector being a class with both angle and speed. The Vector class has a Vector(double,double) constructor so the speed and angle can be set at its initialization but when I make my sprite class. It sends an error that its calling Vector(), a blank constructor, and that it doesn't exist. I'm trying to figure out why its calling Vector(). Here is my code from both the Sprite and Vector classes.

#Vector.h
#ifndef VECTOR_H
#define VECTOR_H

class Vector

{
    public:
        Vector(double,double);
        double getX();
        double getY();
        double getSpeed();
        double getAngle();
        void setSpeed(double);
        void setAngle(double);

    private:
        double speed,angle;
};

#endif


#Vector.h
#include "SDL/SDL.h"
#include "vector.h"
#include "math.h"

Vector::Vector(double speed,double angle)

{
    this -> speed = speed;
    this -> angle = angle;
}

double Vector::getX()

{
    return speed*cos(angle);
}

double Vector::getY()

{
    return speed*sin(angle);
}

double Vector::getSpeed()

{
    return speed;
}

double Vector::getAngle()

{
    return angle;
}

void Vector::setAngle(double angle)

{
    this -> angle = angle;
}

void Vector::setSpeed(double speed)

{
    this -> speed = speed;
}


#Sprite.h:
#ifndef SPRITE_H
#define SPRITE_H
#include "vector.h"

class Sprite

{
    public:
        Sprite(int x,int y);
        SDL_Rect getRect();
        SDL_Surface* getImage();
        void setRect(SDL_Rect);
        void move();
        void draw(SDL_Surface*);

    private:
        Vector movement;
        double x,y,lastX,lastY,angle,speed;
        SDL_Rect rect;
        SDL_Surface* image;
};

#endif


#Sprite.cpp:
#include "SDL/SDL.h"
#include "sprite.h"
#include "functions.h"
#include <cmath>

Sprite::Sprite(int x, int y)

{
    this -> x = x;
    this -> y = y;
    lastX = x;
    lastY = y;
    image = loadImage("box.png");
    rect.x = x;
    rect.y = y;
    rect.w = image->w;
    rect.h = image->h;
    speed = 1;
    angle = 0;
}

SDL_Rect Sprite::getRect()

{   
    return rect;
}

SDL_Surface* Sprite::getImage()

{
    return image;
}

void Sprite::setRect(SDL_Rect rect)
{
    this -> rect = rect;
}

void Sprite::move()

{
    lastX = x;
    lastY = y;
    x += speed*cos(angle);
    y += speed*sin(angle);
    rect.x = int(x);
    rect.y = int(y);
}

void Sprite::draw(SDL_Surface* dest)

{
    blit(image,dest,int(x),int(y));
}
Was it helpful?

Solution

Your Sprite class has a Vector member that will be constructed when the Sprite is constructed. At the moment, the Vector will be initialized with the default constructor because you haven't specified otherwise. If you want a specific constructor of Vector to be used, you need to add an initialization list to the constructor of Sprite:

Sprite::Sprite(int x, int y)
  : movement(1.0, 0.0)
{
  // ...
}

This will initialise movement with arguments 1 and 0. In fact, you might as well add other members to your initialization list too:

Sprite::Sprite(int x, int y)
  : movement(1.0, 0.0), x(x), y(y), lastX(x), lastY(y) // and so on...
{
  // ...
}

OTHER TIPS

The Vector is created in Sprite::Sprite(int x, int y). The blank constructor for Vector is called because you do not call a constructor in the initializer list: in fact, you leave the Vector movement completely uninitialized!

Do this:

Sprite::Sprite(int x, int y):
  movement(3.14, 2.7)
{
  ...
}

to construct movement using a two argument constructor. I would pick better values than 3.14 and 2.7, those are just sample values.

I would also consider creating a public no-argument constructor on Vector for ease of use that initalizes speed and angle to zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top