簡體   English   中英

iostream無法打印到第二個源類中的終端(C ++)

[英]iostream not printing to terminal in second source class (c++)

當我嘗試在構造函數中執行cout時,它不會打印出來。 我知道cout在我的終端上可以正常工作,因為我可以從main()調用它,但是不能從我的CharacterStats.cpp類(帶有CharacterStats.hpp標頭)調用它。

沒有應有的終端輸出。 我希望輸出中出現"---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;

容易犯的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM