完全相同的C++代码提供不同的输出

Man*_*ius -1 c++

所以在提出这个问题之前,我试图找到答案,但我发现的只是人们在代码中出错和/或代码实际上并不相同.

我的问题正是如此 - 相同的代码,唯一的区别是不同的文件名.(两个文件夹,两个项目,不同的文件名,除#include行以外的相同代码)

http://imgur.com/a/5HmzN 代码和输出的比较.左边的代码正常工作,完成后矢量大小为2.

这是代码:(项目A文件名/项目B文件名)

main.cpp中/ main.cpp中

#include <iostream>
#include <vector>
#include "class.h" //this would be "Card.h" in project B

using namespace std;

int main()
{
    cout<<"Start of main"<<endl;
    cout<<K.size()<<endl;
    K.push_back("random STR");
    cout<<K.size()<<endl;
    cout<<"End of main"<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

class.cpp/Card.cpp

#include "class.h" //this would be "Card.h" in project B
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> K;

Card::Card(string card_name         ,
           string card_type         ,
           bool   card_active       ,
           bool   card_discardable  ,
           bool   card_heals        ,
           bool   card_deals_damage ,
           bool   card_draws        ,
           bool   card_blocks       ,
           bool   card_discards     ,
           void (*card_pointer)()   )
{
    Name            = card_name         ;
    Type            = card_type         ;
    Active          = card_active       ;
    Discardable     = card_discardable  ;
    Heals           = card_heals        ;
    Deals_damage    = card_deals_damage ;
    Draws           = card_draws        ;
    Blocks          = card_blocks       ;
    Discards        = card_discards     ;
    Execution       = card_pointer      ;
    cout<<"Start of Class"<<endl;
    cout<<K.size()<<endl;
    K.push_back(Name);
    cout<<K[0]<<endl;
    cout<<K.size()<<endl;
    cout<<"End of Class"<<endl;
}
string Card::getName()
{
    return Name;
}
void Card::execute()
{
    Execution();
}    
Run Code Online (Sandbox Code Playgroud)

class.h/Card.h

#ifndef CARD_H
#define CARD_H
#include <string>
#include <vector>

extern std::vector<std::string> K;

class Card
{
    public:
        Card(   std::string    card_name        ,
                std::string    card_type        ,
                bool           card_active      ,
                bool           card_discardable ,
                bool           card_heals       ,
                bool           card_deals_damage,
                bool           card_draws       ,
                bool           card_blocks      ,
                bool           card_discards    ,
                void (*card_pointer)()          );
        std::string getName();
        void execute();
    private:
        std::string Name        ;
        std::string Type        ;
        bool        Active      ;
        bool        Discardable ;
        bool        Heals       ;
        bool        Deals_damage;
        bool        Draws       ;
        bool        Blocks      ;
        bool        Discards    ;
        void      (*Execution)();
};



#endif
Run Code Online (Sandbox Code Playgroud)

aFile.cpp/TEST.CPP

#include <iostream>
#include "class.h"
void execute_stab()
{
    std::cout<<"You dealt 2 dmg to enemy player"<<std::endl;
}
Card Stab("Stab", "Offensive", false, true, false, true, false, false, false, execute_stab);
Run Code Online (Sandbox Code Playgroud)

输出是在专辑中,对于那些不能在这里使用imgur的人来说:

项目A:

Start of Class
0
1
Stab
End of Class
Start of main
1
2
End of main
Run Code Online (Sandbox Code Playgroud)

项目B:

Start of Class
0
1
Stab
End of Class
Start of main
0
1
End of main
Run Code Online (Sandbox Code Playgroud)

我很抱歉,如果我的代码很乱或我正在以愚蠢的方式做事,我刚刚开始学习C++,这是我尝试创建一个纸牌游戏.所以最大的问题是为什么相同的代码提供不同的输出?谢谢

Som*_*ude 6

全局变量的初始化顺序仅为单个转换单元定义良好.翻译单元之间的顺序是不确定的.这就是这里发生的事情.

你可以不知道,如果KCard.cpp将首先初始化,或Stabtest.cpp.

  • @MantasKandratavicius那是*未定义的行为*给你.有时似乎工作正常,在接下来的一分钟它会崩溃. (3认同)