C ++派生类构造函数调用基类构造函数错误

戈斯

我有一个基类精灵: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());
}

和派生类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";
}

这两个类都使用向量类:

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

现在,我只是想通过以下部分在我的主要代码中使用actor的构造函数:

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

actor a(p, d, 1);

但是运行我的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

如果不在派生类中调用基本构造函数,则我的makefile可以正常工作,因此我确定它在我的makefile中不是错误,但这是我的makefile,以防万一:

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

我假设我在构造函数中语法有些错误,但似乎无法修复,有什么建议吗?

templatetypedef

在头文件中,具有以下行:

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

我在这里看到了您要执行的操作,但这不是合法的C ++语法。如果您打算声明actor构造函数,然后在一个.cpp文件中定义它,这就是您在这里所做的,只需编写

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

然后,在.cpp文件中,完成操作,然后编写

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

正如您现在所写,C ++编译器可以看到actor构造函数的声明然后,当它看到初始化列表(该: sprite(p, d, i)部分)时,会认为“哦,好的,您在这里定义函数”。然后,对于为什么要使用分号而不是函数的实际主体,这感到非常困惑,这就是您要得到的编译器错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在派生类 C++ 的构造函数中调用基类的构造函数

来自分类Dev

C#-在派生类中调用基类和类构造函数

来自分类Dev

如何在C ++中从基类构造函数调用派生类方法?

来自分类Dev

未调用派生类的c ++ / cli静态构造函数

来自分类Dev

派生类的C ++ CRTP构造函数

来自分类Dev

C ++-派生类构造函数的行为

来自分类Dev

如何避免在C ++中的派生类中重复基类构造函数参数?

来自分类Dev

C ++基类构造函数,将派生类作为参数(?)

来自分类Dev

为C ++中的基类和派生类声明“虚拟”构造函数?

来自分类Dev

如何使用基类的数据成员作为派生类的构造函数?(在 C++ 中)

来自分类Dev

C ++:派生类隐式调用显式构造函数

来自分类Dev

代码块C ++类构造函数错误

来自分类Dev

基类的c ++构造函数

来自分类Dev

从派生类构造函数调用基类构造函数

来自分类Dev

从派生类构造函数调用基类构造函数

来自分类Dev

当派生类添加了数据成员时,派生类的构造函数应该如何像c ++中那样

来自分类Dev

为什么派生类的构造函数只能在C ++的类中定义?

来自分类Dev

C++ 可以根据给单个构造函数的参数创建派生类而不是bass 类吗?

来自分类Dev

C ++多态性:派生类调用基类虚拟函数,而不是重写的派生类

来自分类Dev

C ++继承:在标头中调用基类构造函数

来自分类Dev

C#调用基类构造函数的混乱

来自分类Dev

C ++继承:避免调用基类的默认构造函数

来自分类Dev

C ++将派生类的const值传递给基本意外行为的构造函数

来自分类Dev

C ++派生类仅使用继承的基本构造函数的一部分

来自分类Dev

我是否必须为C ++中的派生类编写相同的构造函数?

来自分类Dev

C#根据参数类型实例化基础构造函数的派生类

来自分类Dev

C ++继承:使用派生类的实例调用基类的虚函数

来自分类Dev

从基类构造函数调用派生类的成员函数

来自分类Dev

派生类中的构造函数错误

Related 相关文章

  1. 1

    在派生类 C++ 的构造函数中调用基类的构造函数

  2. 2

    C#-在派生类中调用基类和类构造函数

  3. 3

    如何在C ++中从基类构造函数调用派生类方法?

  4. 4

    未调用派生类的c ++ / cli静态构造函数

  5. 5

    派生类的C ++ CRTP构造函数

  6. 6

    C ++-派生类构造函数的行为

  7. 7

    如何避免在C ++中的派生类中重复基类构造函数参数?

  8. 8

    C ++基类构造函数,将派生类作为参数(?)

  9. 9

    为C ++中的基类和派生类声明“虚拟”构造函数?

  10. 10

    如何使用基类的数据成员作为派生类的构造函数?(在 C++ 中)

  11. 11

    C ++:派生类隐式调用显式构造函数

  12. 12

    代码块C ++类构造函数错误

  13. 13

    基类的c ++构造函数

  14. 14

    从派生类构造函数调用基类构造函数

  15. 15

    从派生类构造函数调用基类构造函数

  16. 16

    当派生类添加了数据成员时,派生类的构造函数应该如何像c ++中那样

  17. 17

    为什么派生类的构造函数只能在C ++的类中定义?

  18. 18

    C++ 可以根据给单个构造函数的参数创建派生类而不是bass 类吗?

  19. 19

    C ++多态性:派生类调用基类虚拟函数,而不是重写的派生类

  20. 20

    C ++继承:在标头中调用基类构造函数

  21. 21

    C#调用基类构造函数的混乱

  22. 22

    C ++继承:避免调用基类的默认构造函数

  23. 23

    C ++将派生类的const值传递给基本意外行为的构造函数

  24. 24

    C ++派生类仅使用继承的基本构造函数的一部分

  25. 25

    我是否必须为C ++中的派生类编写相同的构造函数?

  26. 26

    C#根据参数类型实例化基础构造函数的派生类

  27. 27

    C ++继承:使用派生类的实例调用基类的虚函数

  28. 28

    从基类构造函数调用派生类的成员函数

  29. 29

    派生类中的构造函数错误

热门标签

归档