C++ 错误:在带有参数的构造函数中'*'令牌之前预期的主表达式

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

本文关键字:令牌 表达式 错误 参数 构造函数 C++      更新时间:2023-10-16

我正在尝试创建一个具有五个参数的构造函数的类。构造函数所做的唯一一件事就是将所有参数传递给超类构造函数。这个类没有任何额外的变量:它的唯一目的是更改getClassType虚函数的实现。由于某种原因,该头文件在构造函数中提供了"在'*'标记之前的预期主表达式",并且在同一行中还提供了四个"在'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

我认为这些错误是由一些打字错误引起的,但我已经检查了教程和谷歌,我没有注意到任何错误,所以我卡住了

不能将类型传递给基类构造函数:

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){}

您不应该为超类构造函数调用重复类型。

Suelo(b2World *world,int x,int y,int w,int h) : Plataforma(world, x, y, w, h){}