简体   繁体   中英

C++ derived class constructor call base class constructor errors

I have a base class sprite: sprite.hh

#include <iostream>
#include "vector.hh"

#ifndef SPRITE
#define SPRITE

class sprite
{
    private:
        vector pos;
        //the width and height of the sprite
        vector dimensions;
        int imArrIndex;
        
    public:
        sprite();
        sprite(vector p, vector d, int i);
        void setPos(vector v);
        vector getPos();
        void setDimensions(vector v);
        vector getDimensions();
        void setImArrIndex(int i);
        int getImArrIndex();
        void movePos(vector v);
};
#endif

sprite.cc:

#include <iostream>
#include "sprite.hh"
#include "vector.hh"

using namespace std;

sprite::sprite()
{
    cout << "sprite created\n";
}

sprite::sprite(vector p, vector d, int i)
{
    pos = p;
    dimensions = d;
    imArrIndex = i;
}

void sprite::setPos(vector v)
{
    pos = v;
}

vector sprite::getPos()
{
    return pos;
}

void sprite::setDimensions(vector v)
{
    dimensions = v;
}

vector sprite::getDimensions()
{
    return dimensions;
}

void sprite::setImArrIndex(int i)
{
    imArrIndex = i;
}

int sprite::getImArrIndex()
{
    return imArrIndex;
}

void sprite::movePos(vector v)
{
    pos.setX(pos.getX() + v.getX());
    pos.setY(pos.getY() + v.getY());
}

and a derived class actor: actor.hh:

#include <iostream>
#include "sprite.hh"
#include "vector.hh"

#ifndef ACTOR
#define ACTOR

class actor : public sprite
{
    private:
        
    public:
        actor(vector p, vector d, int i) : sprite(p, d, i);
};
#endif

actor.cc:

#include <iostream>
#include "sprite.hh"
#include "actor.hh"
#include "vector.hh"

using namespace std;

actor::actor(vector p, vector d, int i) : sprite(p, d, i)
{
    cout << "actor created\n";
}

Both classes use the vector class:

vector.hh:

#include <iostream>
#include <cmath>
#ifndef VECTOR
#define VECTOR

class vector
{
    private:
        int x;
        int y;
    public:
        vector();
        vector(int px, int py);
        void setX(int px);
        int getX();
        void setY(int py);
        int getY();
        //get the pixel distance between this vector and a given vector
        int getDistance(vector v);
};
#endif

vector.cc:

#include <iostream>
#include "vector.hh"

using namespace std;

vector::vector()
{
    cout << "vector created\n";
}

vector::vector(int px, int py)
{
    x = px;
    y = py;
    cout << "vector created\n";
}
void vector::setX(int px)
{
    x = px;
}
int vector::getX()
{
    return x;
}
void vector::setY(int py)
{
    y = py;
}
int vector::getY()
{
    return y;
}
//get the pixel distance between this vector and a given vector
int vector::getDistance(vector v)
{
    return sqrt( pow(x - v.getX(), 2) + pow(y - v.getY(), 2) );
}

Right now I am just trying to get the constructor for the actor working in my main code with this section:

vector p(0,0);
vector d(0,0);

actor a(p, d, 1);

But I am getting this error when running my makefile:

g++ -c level.cc
In file included from level.cc:6:
actor.hh: In constructor ‘actor::actor(vector, vector, int)’:
actor.hh:15:53: error: expected ‘{’ at end of input
   actor(vector p, vector d, int i) : sprite( p, d, i);
                                                     ^
make: *** [makefile:11: level.o] Error 1

My makefile works just fine if I don't call the base constructor in the derived class so I'm sure its not an error in my makefile, but here is my makefile just in case:

FrogGame: frogmain.o game.o level.o sprite.o actor.o vector.o
    g++  -std=c++0x -Wall -pedantic -o FrogGame frogmain.o game.o level.o sprite.o actor.o vector.o  `sdl2-config --cflags --libs` -lSDL2_image
    
frogmain.o: frogmain.cc game.hh level.hh sprite.hh vector.hh
    g++ -c frogmain.cc
    
game.o: game.cc level.hh sprite.hh actor.hh vector.hh
    g++ -c game.cc
    
level.o: level.cc sprite.hh actor.hh vector.hh
    g++ -c level.cc

actor.o: actor.cc vector.hh
    g++ -c actor.cc

sprite.o: sprite.cc vector.hh
    g++ -c sprite.cc

vector.o: vector.cc
    g++ -c vector.cc

I'm assuming I have some syntax wrong in the constructor but can't seem to fix it, any suggestions?

In your header file, you have this line:

public:
    actor(vector p, vector d, int i) : sprite(p, d, i); // Error!

I see what you're trying to do here, but this isn't legal C++ syntax. If you plan to declare the actor constructor but then define it in a .cpp file, which is what you're doing here, just write

actor(vector p, vector d, int i);

Then, in the .cpp file, as you've done, write

actor::actor(vector p, vector d, int i) : sprite(p, d, i) 
{
    cout << "actor created\n"; 
}

As you have it written right now, the C++ compiler sees the declaration of the actor constructor. When it then sees the initialization list (the : sprite(p, d, i) part), it thinks "oh, okay, you're defining the functions here." It's then mightily confused about why there's a semicolon instead of the actual body of the function, which is what the compiler error you're getting says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM