简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM