当我尝试在构造函数中执行指令时,它不会打印出来。我知道cout在我的终端上可以正常工作,因为我可以从main()调用它,但是不能从我的CharacterStats.cpp类(带有CharacterStats.hpp header )调用它。
没有应有的终端输出。我期望"---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;
容易犯的错误。