c++ - iostream not printing to terminal in second source class (c++)
问题描述
When I try to do a cout in a constructor it will not print out. I know cout works on my terminal because I can call it from the main(), but not from my CharacterStats.cpp class with a CharacterStats.hpp header.
There is no terminal output like there should be. I am expecting "---DATALESS UNIT CREATED---"
to appear in the output
I use
g++ -o a main.cpp CharacterStats.cpp CharacterStats.hpp
./a
to compile and execute and nothing print out
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 */
解决方案
It's because you aren't calling your constructor.
CharacterStats coreUser();
declares a function taking no arguments and returning a CharacterStats
.
What you want is
CharacterStats coreUser;
Easy mistake to make.
推荐阅读
- c# - 如何在 asp.net-core 中将 URL 重写为子域,而无需在 dot net core 上实际创建子域
- python - 是否可以使用文本包含通过它的 CSS 来查找元素?
- python - Python循环遍历元组列表,从熊猫数据框中添加一个值
- r - 根据向量中包含的多个条件创建向量
- kubernetes - 如何先创建命名空间?
- guice - 构造函数有附加参数时关于guice构造注入的问题
- r - R Shiny plot透明背景:低分辨率(轴文本和标签周围的白色像素,以及标题)
- reporting-services - SSRS - 将分组数据放在同一行
- excel - SharePoint 公式:[已创建]+60 天 = 延期日期(但需要是工作日)
- powerbi - PowerBI 中选项卡/页面的基于角色的安全性(不是行级安全性)