当我尝试在构造函数中执行 cout 时,它不会打印出来。我知道 cout 在我的终端上工作,因为我可以从 main() 调用它,但不能从带有 CharacterStats.hpp header 的 CharacterStats.cpp 类调用它。
没有应有的终端输出。我期待 "---DATALESS UNIT CREATED---"
出现在输出中
我用
g++ -o a main.cpp CharacterStats.cpp CharacterStats.hpp
./a
编译执行,什么都不打印
main.cpp
#include "CharacterStats.hpp"
int main(void){
CharacterStats coreUser();
return 0;
}
CharacterStats.cpp
#include "CharacterStats.hpp"
#include <iostream>
using namespace std;
CharacterStats::CharacterStats(char name, bool type, short strength, short armor, short resist, short speed, short luck){
cout << "---CORE DECLARED---" << endl;
this->name = name;
this->type = type;
this->strength = strength;
this->armor = armor;
this->resist = resist;
this->speed = speed;
this->luck = luck;
}
CharacterStats::CharacterStats(){
cout << "---DATALESS UNIT CREATED---" << endl;
}
CharacterStats.hpp
#ifndef CHARACTER_STATS
#define CHARACTER_STATS
class CharacterStats{
private:
char name;
bool type;
short strength, armor, resist, speed, luck;
public:
CharacterStats(char, bool, short, short, short, short, short);
CharacterStats();
};
#endif /* CHARACTER_STATS */
最佳答案
这是因为您没有调用构造函数。
CharacterStats coreUser();
声明一个函数,不带任何参数并返回一个CharacterStats
。
你想要的是
CharacterStats coreUser;
容易犯的错误。
关于c++ - iostream 不打印到第二个源类中的终端 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56917518/