全局变量错误C ++

时间:2016-11-27 05:58:40

标签: c++ operating-system global-variables

我有一个错误,我不知道如何修复我的大型操作系统作业。我得到的错误是" 42个用于架构x86_64"的重复符号。我认为这与我的全局变量文件" global.h"有关。我使用了3个全局变量," global.h"包含在名为" PageReplacementAlgorithm.cpp"的抽象类中。我有大约6个派生自PageReplacementAlgorithm类的类,它们使用这些全局变量。我认为当我在我的" main.cpp"中包含所有这些派生类时会出现问题。因为我需要制作它们的新实例。如何修复全局变量的实现?

Global.h

#include "PageTableEntry.h"
using namespace std;
#ifndef Global_H
#define Global_H

extern PageTableEntry pageTable[64];
extern int* frameTable;
extern int framesCount;

#endif

PageReplacementAlgorithm.h

#include "Global.h"
using namespace std;

#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H

class PageReplacementAlgorithm {
public:
   virtual int selectFrame(PageTableEntry &p) = 0;
};

#endif

示例派生类(FIFO)

include "PageReplacementAlgorithm.h"
using namespace std;

#ifndef FIFO_H
#define FIFO_H

class FIFO : public PageReplacementAlgorithm {
public:
    FIFO();
    int selectFrame(PageTableEntry &p);
private:
     int entries;
};

#endif

Main.cpp的

 #include "Aging.cpp"
 #include "Clock.cpp"
 #include "FIFO.cpp"
 #include "MMU.cpp"
 #include "NRU.cpp"
 #include "Random.cpp"
 #include "SecondChance.cpp"

2 个答案:

答案 0 :(得分:1)

为什么要在main.cpp中包含所有cpp文件?我认为它们包含相同的内容,对吧?即使你有那里的守卫,你在那些守卫之前做了额外的包括,这可能是问题的根源。 main.cpp可以只包含类的main()函数和import头,不需要包含cpp。

此外,您可以将头文件修改为这样(为了极端安全):

#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
#include "Global.h"
using namespace std;

...
#endif

我建议您查看答案C++ #include guards

答案 1 :(得分:0)

如果你摆脱#include "(anything).cpp,事情应该会好得多。在构建项目时,或运行编译器,例如g++ main.cpp foo.cpp,即那些.cpp文件构建并链接到您的程序中的时间。

相关问题