Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error: expected primary expression before '*' token in constructor with parameters

I'm trying to make a class that has a constructor with five parameters. The only thing the constructor does is pass all the parameters to the superclass constructor. This class doesn't have any extra variables: its only purpose is to change the implementation of the getClassType virtual function. For some reason, this header gives an "expected primary expression before '*' token" on line with the constructor, and it also gives four "expected primary expression before 'int'" on the same line:

#ifndef SUELO_H
#define SUELO_H

#include "plataforma.h"
#include "enums.h"
#include "object.h"
#include "Box2D/Box2D.h"


class Suelo : public Plataforma
{
public:
    Suelo(b2World *world,int x,int y,int w,int h) : Plataforma(b2World* world,int x,int y,int w,int h){}
    virtual ~Suelo();
    virtual ClassType getClassType();
protected:
private:
};

#endif // SUELO_H

I assume these errors are caused by some typo, but I've checked on tutorials and on Google and I don't notice any mistake, so I'm stuck.

like image 665
Magnus Avatar asked Aug 29 '26 04:08

Magnus


1 Answers

You don't pass the types into the base class constructor:

class A
{
    public:
    A(int) {};
}

class B : public A
{
public:
    B(int x) : A(x)  // notice A(x), not A(int x)
    {}
};

So, you constructor should look like this:

Suelo(b2World *world,int x,int y,int w,int h) : Plataforma(world,x,y,w,h){}
like image 133
Chad Avatar answered Aug 30 '26 17:08

Chad