我正在尝试创建一个具有带五个参数的构造函数的类。构造函数唯一做的就是将所有参数传递给父类(super class)构造函数。这个类没有任何额外的变量:它的唯一目的是改变 getClassType 虚函数的实现。出于某种原因,此 header 与构造函数一起给出了“'*'标记之前的预期主表达式”,并且还在同一行上给出了四个“'int'之前的预期主表达式”:
#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
我假设这些错误是由某些拼写错误引起的,但我已经检查了教程和 Google,但没有发现任何错误,所以我被卡住了。
最佳答案
您不将类型传递给基类构造函数:
class A
{
public:
A(int) {};
}
class B : public A
{
public:
B(int x) : A(x) // notice A(x), not A(int x)
{}
};
所以,你的构造函数应该是这样的:
Suelo(b2World *world,int x,int y,int w,int h) : Plataforma(world,x,y,w,h){}
关于C++ 错误 : expected primary expression before '*' token in constructor with parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6638666/