【问题标题】:C++ Error: expected primary expression before '*' token in constructor with parametersC++ 错误:在带有参数的构造函数中的“*”标记之前的预期主表达式
【发布时间】:2011-07-10 01:49:36
【问题描述】:

我正在尝试创建一个具有五个参数的构造函数的类。构造函数唯一要做的就是将所有参数传递给超类构造函数。这个类没有任何额外的变量:它的唯一目的是改变 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

我认为这些错误是由一些拼写错误引起的,但我检查了教程和谷歌,我没有发现任何错误,所以我被卡住了。

【问题讨论】:

    标签: c++ constructor compiler-errors


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-03-31
        • 2012-07-08
        • 2015-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 2013-01-13
        相关资源
        最近更新 更多