0

学校向けのプロジェクトに取り組んでいます。私は循環依存関係を認識しています (そして、以前にその解決策のほとんどをここで読んだことがあります) が、現在は必要な方法で機能しています。悲しいことに、それが私の悩みの原因でもあると確信しています。両方のファイルで使用できるように含め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 Stuff\Fall 2015\CIST 2362 C++ II\Final - Airline Reservation System\Debug\Final - 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

BoardingPass.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 
4

2 に答える 2

1

変数を宣言するのではconcol.hなく、名前空間内で変数を定義しています。extern変数を編集する必要があります

extern HANDLE std_con_out;

ヘッダーで (それらを定義するため)、次に .cpp ファイル (concol.cpp) で (extern なしで) 宣言します。

于 2015-11-21T03:33:04.230 に答える
0

リンカーによって「既に定義されている」と示されるシンボルは、ヘッダー ファイルで宣言されているが、クラスの外部にある項目です。(「ファイル スコープ」で。) その結果、これらのヘッダー ファイルを含むすべての .cpp ファイルは、それらのストレージを再定義しようとします。リンカがオブジェクト ファイルを一緒にリンクしようとすると、これらの複数の定義が表示され、それらについて不平を言います。

問題を解決するための最善の方法は、これらすべての項目をクラスの「静的データ メンバー」にすることです。これは、クラス内でそれらを宣言し、 としてマークすることを意味しますstatic

それらをクラス内に移動したくない場合は、1 つを除くすべての .cpp ファイルに含まれる場合、それらが「extern」として宣言されるようにする必要があります。このように、1 つの .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.cpp がステートメントをコンパイルします。

int my_integer;

b.cpp、c.cppなどはステートメントをコンパイルします

extern int my_integer;
于 2015-11-21T03:33:30.517 に答える