【问题标题】:C++ cin and cout not showing in consoleC++ cin 和 cout 未显示在控制台中
【发布时间】:2013-09-17 17:06:33
【问题描述】:

我和一些朋友开始了一个游戏项目,我们试图找出为什么我们的课程没有显示任何输出。我们已包含 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;
}

【问题讨论】:

  • 对我来说,Tile_Gun testgun(texId, dmg); std::cout &lt;&lt; testgun; 似乎更明智。
  • 以上代码中的cins和couts在哪里?
  • 我在底部添加了 cins 和 couts =)
  • 添加新行:cout &lt;&lt; "stuff" &lt;&lt; endl; 默认情况下,控制台很可能是行缓冲的。
  • 在我调用 Print_Data() 之前的 main() 中?编辑:无论在哪里,它都不会出现!

标签: c++ inheritance iostream


【解决方案1】:

您的代码中的 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;
}

【讨论】:

  • 我现在找到了答案!我的项目未设置为运行单独的控制台窗口。这就是为什么只有 system("pause") 出现...我的解决方案是:项目设置 -> 链接器 -> 子系统 = 控制台 (/SUBSYSTEM:CONSOLE)
  • @AleksanderFimreite 是的,这也可能是问题所在。但这应该给出error LNK2019,你说,当我之前问过时,它没有给出任何错误。
  • 嗯,就是这样...为什么它没有给我一个错误,我不知道。但这是解决方案;)
【解决方案2】:

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

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

【讨论】:

  • 为什么你认为构造函数有问题? OP 没有显示他的构造函数的定义。
  • 部分是因为他没有给他们看。使用添加的构造函数运行他的代码似乎会产生他正在寻找的响应,所以我只是假设。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2015-11-13
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多