0

我和一些朋友开始了一个游戏项目,我们试图找出为什么我们的课程没有显示任何输出。我们已包含 SDL 2.0(只是让您知道,以防万一)

问题是我们有一个继承和填充的类......

class Tile {
private:
    int textureId;
public:
    Tile();
    Tile(int texId);
    ~Tile();

    void Print_Data();
}

class Tile_Gun : public Tile {
private:
    int damage;
public:
    Tile_Gun();
    Tile_Gun(int texId, int dmg);
    ~Tile_Gun();

    void Print_Data();
}

那是基础设置。我想为两者运行 Print_Data() 。我在 main() 中创建了一个对象,并设置了断点来控制数据,这些数据似乎都停止并填充了预期的区域。但是当它启动 Print_Data() 函数时,它会在断点中的 couts 和 cins 处停止(它会运行它),但不会将任何内容添加到控制台中。

发生了什么事,如果您需要更多信息,请告诉我......(我想我现在尽可能简短)

我如何称呼班级:

int texId = 0, dmg = 5;
Tile_Gun testgun = Tile_Gun(texId, dmg);
//The 0 passed to the parent constructor with Tile::Tile(texId)
testgun.Print_Data();

编辑:

void Tile::Print_Data() {
    int dummy;
    cout << "My texId is: " << textureId;
    cin >> dummy;
}

void Tile_Gun::Print_Data() {
    int dummy;
    cout << "My damage is: " << damage;
    cin >> dummy;
}
4

2 回答 2

0

您的代码中没有关于iostream的问题。这是我尝试运行您的代码的方式,并且运行良好。检查你的构造函数和析构函数。确保构造函数实现。参考以下代码。

#include "stdafx.h"
#include <iostream>
using namespace std;

class Tile {
protected:
int textureId;
public:
Tile(){
}
Tile(int texId)
{
    textureId = texId;
}
// ~Tile();
void Print_Data();

};

void Tile::Print_Data() {
int dummy;
cout << "My texId is: " << textureId;
cin >> dummy;
}

class Tile_Gun : public Tile {
private:
int damage;
public:
Tile_Gun()
{

}
Tile_Gun(int texId, int dmg)
{
    damage = dmg;
    textureId = texId;
}
//  ~Tile_Gun();

void Print_Data();
};
void Tile_Gun::Print_Data() {
int dummy;
cout << "My damage is: " << damage<<endl;
cin >> dummy;
cout<<"I have taken input: "<<dummy<<endl;
}





int _tmain(int argc, _TCHAR* argv[])
{
int texId = 0, dmg = 5;
Tile_Gun testgun = Tile_Gun(texId, dmg);
//The 0 passed to the parent constructor with Tile::Tile(texId)
testgun.Print_Data();
return 0;
}
于 2013-09-17T17:48:17.827 回答
-1

我认为您的构造函数有问题。这是一个快速修复,不确定您想要发生什么。但是你什么也没得到,因为默认构造函数没有分配给这些变量。

#include<iostream>


class Tile {
private:
    int textureId;
public:
    Tile();
    Tile(int texId);

    void Print_Data();
};
Tile::Tile() {
    textureId=0;
}

Tile::Tile(int texId) {
    textureId=texId;
}

class Tile_Gun : public Tile {
private:
    int damage;
public:
    Tile_Gun();
    Tile_Gun(int texId, int dmg);

    void Print_Data();
};

Tile_Gun::Tile_Gun() {
    damage=0;
}

Tile_Gun::Tile_Gun(int texId, int dmg) {
    damage=dmg;
}

void Tile::Print_Data() {
    int dummy;
    std::cout << "My texId is: " << textureId;
    std::cin >> dummy;
}

void Tile_Gun::Print_Data() {
    int dummy;
    std::cout << "My damage is: " << damage;
    std::cin >> dummy;
}

void main() {
    int texId = 0, dmg = 5;
    Tile_Gun testgun = Tile_Gun(texId, dmg);
    //The 0 passed to the parent constructor with Tile::Tile(texId)
    testgun.Print_Data();
}
于 2013-09-17T17:43:53.917 回答