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

时间:2017-01-27 19:34:56

标签: 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;
}

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();
}    

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

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);

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

项目A:

Start of Class
0
1
Stab
End of Class
Start of main
1
2
End of main

项目B:

Start of Class
0
1
Stab
End of Class
Start of main
0
1
End of main

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

1 个答案:

答案 0 :(得分:6)

全局变量的初始化顺序仅为单个translation unit定义良好。翻译单元之间的顺序是未定义。这就是这里发生的事情。

您无法知道K中的Card.cpp是先被初始化,还是Stab来自test.cpp

相关问题