1

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 */
hevansa98
  • 87
  • 1
  • 1
  • 8
  • 2
    You shouldn't pass header files to the compiler. Headers are for code directly shared by different cpp files, not for compiling directly into their own object file. – chris Jul 06 '19 at 20:43
  • @chris So I should only pass them to the compiler when shared by multiple source files? Do you have a reference I can look at to learn more about that? – hevansa98 Jul 06 '19 at 20:46
  • 1
    @hevansa98 No you should *never* compile a header file directly. The correct way to build your code is `g++ -o a main.cpp CharacterStats.cpp` – john Jul 06 '19 at 20:47
  • @chris I was reading that when the header is compiled it allows for quicker future compiles (precompiled) so if I was satisfied with the header wouldn't I then compile it? For a project such as mine, I doubt a few microseconds would matter but still – hevansa98 Jul 06 '19 at 20:50
  • 1
    Possible duplicate of [Default constructor with empty brackets](https://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) – Joseph Sible-Reinstate Monica Jul 06 '19 at 20:50
  • I honestly can't think of a single time where it's recommended to compile a header directly. The header is indirectly compiled twice by compiling the two cpp files and compiling it separately simply goes to waste. The only situation that comes close is verifying that the header is self-contained, but the going advice there is to have a cpp file that includes it on the first line. The best reference I can really give for that is suitable introductory material on C++, such as a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – chris Jul 06 '19 at 20:51
  • @hevansa98 Precompiled header files are a different thing and precompilation is not the same as compilation. Your header files are compiled by being included in your source files, that what they are for. If you also compile your header files then what exactly is the difference between header files and source files? – john Jul 06 '19 at 20:52
  • Regarding precompiling a header, compilers can offer that option, but it isn't done like this. The object file you produce by compiling is for linking purposes, to provide one place for a function etc. to exist. Any object file produced by a header should be empty. Otherwise each file including that header would have duplicate definitions. – chris Jul 06 '19 at 20:53
  • @chris Ahh gotcha, I see the redundancy, thank you both! Thank you as well for the reference! – hevansa98 Jul 06 '19 at 20:56

1 Answers1

2

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.

john
  • 85,011
  • 4
  • 57
  • 81