添加新头文件时出现C++错误LNK2005



我正在为学校做一个项目。我意识到我的循环依赖性(以前也读过这里的大多数决议),但目前它正以我需要的方式发挥作用。遗憾的是,我很确定这也是我痛苦的原因。我想包括concol.h,这样它就可以与两个文件一起使用(我想为我的输出添加一些颜色——这不是我的任务要求,而是我想做的事情)。我试过把这个头文件放在几个不同的位置,但总是会出现同样的错误。我考虑过像处理循环依赖关系那样使用正向声明,但我认为这对命名空间不起作用。

错误:

1>Flight.obj : error LNK2005: "void * eku::std_con_out" (?std_con_out@eku@@3PAXA) already defined in BoardingPass.obj 1>Flight.obj : error LNK2005: "bool eku::colorprotect" (?colorprotect@eku@@3_NA) already defined in BoardingPass.obj 1>Flight.obj : error LNK2005: "enum eku::concol eku::textcol" (?textcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Flight.obj : error LNK2005: "enum eku::concol eku::backcol" (?backcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Flight.obj : error LNK2005: "enum eku::concol eku::deftextcol" (?deftextcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Flight.obj : error LNK2005: "enum eku::concol eku::defbackcol" (?defbackcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "void * eku::std_con_out" (?std_con_out@eku@@3PAXA) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "bool eku::colorprotect" (?colorprotect@eku@@3_NA) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "enum eku::concol eku::textcol" (?textcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "enum eku::concol eku::backcol" (?backcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "enum eku::concol eku::deftextcol" (?deftextcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>Source.obj : error LNK2005: "enum eku::concol eku::defbackcol" (?defbackcol@eku@@3W4concol@1@A) already defined in BoardingPass.obj 1>D:School StuffFall 2015CIST 2362 C++ IIFinal - Airline Reservation SystemDebugFinal - Airline Reservation System.exe : fatal error LNK1169: one or more multiply defined symbols found

来源.cpp

#include <fstream>
#include <iostream>
#include <iomanip>
#include "FlightComparators.h" //includes Flight.h
#include "LocationComparators.h"
//prototypes
//methods

飞行.h

#ifndef FLIGHT_H
#define FLIGHT_H
#ifndef BOARDINGPASS_H
#include "BoardingPass.h"
#endif
#include <algorithm>
#include <string>
#include <vector>
#include "Location.h"
#include "Validate.h"
#include "AirlineTypeA.h"
#include "AirlineTypeB.h"
class BoardingPass;
class Flight{
private:
    Location *departureLoc;
    Location *destinationLoc;
    char departureTime[6];
    char arrivalTime[6];
    int number;
    int freqFlyerMiles;
    int curOccupancy = 0;
    Airline *plane;
    vector<BoardingPass*> passengers;
public:
    //constructor
    Flight(Location*, Location*, string, string, int, int, char type);
    //getters
    Location* getDepartureLoc(){ return departureLoc; }
    Location* getDestinationLoc(){ return destinationLoc; }
    int getFlightNumber(){ return number; }
    int getFreqFlyerMiles(){ return freqFlyerMiles; }
    string getDepTime(){ return departureTime; }
    string getAriTime(){ return arrivalTime; }
    int getCurOccupancy(){ return curOccupancy; }
    Airline* getPlane(){ return plane; }
    vector<BoardingPass*> getPassengerList(){ return passengers; }
    bool getIsFull(){ return this->plane->getMaxPass() > curOccupancy; }
    void addPass(string, string, string);
    void cancelReservation(int);
    void displayPassengers();
    void sortPassengers();
};
#endif

登机牌.h

#ifndef BOARDINGPASS_H
#define BOARDINGPASS_H
#ifndef FLIGHT_H
#include "Flight.h"
#endif
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Flight;
class BoardingPass{
private:
    string fName;
    string lName;
    Flight* flight;
    string seat;
public:
    BoardingPass(string, string, Flight *flt, string seat);
    string getFName(){ return fName; }
    string getLName(){ return lName; }
    string getSeat(){ return seat; }
    void displayBoardingPass();
    void writeBoardingPass(fstream&);
};
#endif

concol.h

#ifndef INC_EKU_IO_CONCOL
#define INC_EKU_IO_CONCOL
/*Header file to color text and background in windows console applications
Global variables - textcol,backcol,deftextcol,defbackcol,colorprotect*/
#include<windows.h>
#include<iosfwd>
namespace eku
{
#ifndef CONCOL
#define CONCOL
    enum concol
    {
        black = 0,
        dark_blue = 1,
        dark_green = 2,
        dark_aqua, dark_cyan = 3,
        dark_red = 4,
        dark_purple = 5, dark_pink = 5, dark_magenta = 5,
        dark_yellow = 6,
        dark_white = 7,
        gray = 8,
        blue = 9,
        green = 10,
        aqua = 11, cyan = 11,
        red = 12,
        purple = 13, pink = 13, magenta = 13,
        yellow = 14,
        white = 15
    };
#endif //CONCOL
    HANDLE std_con_out;
    //Standard Output Handle
    bool colorprotect = false;
    //If colorprotect is true, background and text colors will never be the same
    concol textcol, backcol, deftextcol, defbackcol;
    /*textcol - current text color
    backcol - current back color
    deftextcol - original text color
    defbackcol - original back color*/
    inline void update_colors()
    {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo(std_con_out, &csbi);
        textcol = concol(csbi.wAttributes & 15);
        backcol = concol((csbi.wAttributes & 0xf0) >> 4);
    }
    inline void setcolor(concol textcolor, concol backcolor)
    {
        if (colorprotect && textcolor == backcolor)return;
        textcol = textcolor; backcol = backcolor;
        unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
        SetConsoleTextAttribute(std_con_out, wAttributes);
    }
    inline void settextcolor(concol textcolor)
    {
        if (colorprotect && textcolor == backcol)return;
        textcol = textcolor;
        unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
        SetConsoleTextAttribute(std_con_out, wAttributes);
    }
    inline void setbackcolor(concol backcolor)
    {
        if (colorprotect && textcol == backcolor)return;
        backcol = backcolor;
        unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
        SetConsoleTextAttribute(std_con_out, wAttributes);
    }
    inline void concolinit()
    {
        std_con_out = GetStdHandle(STD_OUTPUT_HANDLE);
        update_colors();
        deftextcol = textcol; defbackcol = backcol;
    }
    template<class elem, class traits>
    inline std::basic_ostream<elem, traits>& operator<<(std::basic_ostream<elem, traits>& os, concol col)
    {
        os.flush(); settextcolor(col); return os;
    }
    template<class elem, class traits>
    inline std::basic_istream<elem, traits>& operator>>(std::basic_istream<elem, traits>& is, concol col)
    {
        std::basic_ostream<elem, traits>* p = is.tie();
        if (p != NULL)p->flush();
        settextcolor(col);
        return is;
    }
}   //end of namespace eku
#endif  //INC_EKU_IO_CONCOL 

concol.h中,您在命名空间中定义变量,而不是声明它们。变量需要是extern ed

extern HANDLE std_con_out;

在头中(定义它们),然后在.cpp文件(concol.cpp)中声明(不带extern)。

链接器指示为"已定义"的符号是在头文件中声明的项,但在任何类之外。(在"文件范围"中。)因此,包括这些头文件的每个.cpp文件都试图为它们重新定义存储。当链接器试图将对象文件链接在一起时,它会看到这些多个定义,并抱怨它们。

解决问题的最佳方法是使所有这些项成为类的"静态数据成员"。这意味着在类中声明它们,并将它们标记为static

如果您不想在类中移动它们,那么您必须确保当除了一个.cpp文件之外的所有.cpp文件都包含它们时,它们将被声明为"extern"。这样,只有一个.cpp文件会尝试为它们定义存储,链接器会很高兴。

这通常如下所示:

a.cpp:

#define DECLARE_STORAGE
#include "myheader.h"

b.cpp、c.cpp等:

#include "myheader.h"  //without defining DECLARE_STORAGE

myheader.h

#ifdef DECLARE_STORAGE
#define POSSIBLY_EXTERN
#else
#define POSSIBLY_EXTERN extern
#endif
POSSIBLY_EXTERN int my_integer;

这将导致a.pp编译语句

int my_integer;

而b.cpp、c.cpp等将编译语句

extern int my_integer;

相关内容

  • 没有找到相关文章

最新更新