2

このエラーが発生しましたが、以下のエラーで述べたように「int const ITEMS」が DisplayErrorMessage.cpp で宣言されていないため、理解できません。

1>Main.obj : error LNK2005: "int const ITEMS" (?ITEMS@@3HB) already defined in DisplayErrorMessage.obj
1>MakeSelection.obj : error LNK2005: "int const ITEMS" (?ITEMS@@3HB) already defined in DisplayErrorMessage.obj
1>ShowMenu.obj : error LNK2005: "int const ITEMS" (?ITEMS@@3HB) already defined in DisplayErrorMessage.obj
1>c:\users\kanaan\documents\visual studio 2010\Projects\Assign2\Debug\Assign2.exe : fatal error LNK1169: one or more multiply defined symbols found

コードは次のとおりです。

ヘッダーファイル

#ifndef _VENDINGMACHINE_H_
#define _VENDINGMACHINE_H_

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

  //extern int Denominations;
  extern int const ITEMS = 9;

  extern int Coins[5];
  extern int NumCoins[5]; //assume we have 10 coins of each denomination

   extern int ItemPrice[ITEMS ]; //price in cents
   extern int NumItems[ITEMS]; 


//extern double Total_Price;
//extern double Item_Total;

class VendingMachine{
public:
    void MakeSelection();
    int ReturnChange(int change, int Coins[], int NumCoins[]);
    void ShowMenu();
    void DisplayErrorMessage(int error);
    void PrintConfidentialInformation(int Denominations, int Items, int Coins[], 
                                                    int NumCoins[], int ItemPrice[] , int NumItems[]);

private:
    int selection;
    string code;
    double Each_Item[ITEMS];       //price for each item
};
#endif //_VENDING_MACHINE_H_

cpp ファイルは次のとおりです。MakeSelection.cpp #include "Vending Machine.h"

void VendingMachine::MakeSelection(){



   //assume we have 10 coins of each denomination

  double Total_Price;
// double Item_Total;

  int Coins[5] = {100, 50, 20, 10, 5};
   int NumCoins[5] = {10, 10, 10, 10, 10}; 
    int ItemPrice[ITEMS] = { 75, 120, 120, 100, 150, 95, 110, 50, 120 }; //price in cents
  int NumItems[ ITEMS] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };

    string Product[ITEMS] = {"Water","Coke","Diet Coke","Iced Tea","Swiss Chocolate","Candy",
                                                    "Chips","Bubble Gum","Turkish Delight"};
    int b = 0;
int a = 1;

cout << "Please enter the number of your choice from the menu above. " << endl;

do{
    cout << "\nEnter the number of product OR End transaction: " << endl;
    cin >> selection;
    cout << "\nYou have selected " <<Product[selection] << endl;

    if(selection >= 1 && selection <= 9){

        NumItems[selection - 1] = NumItems[selection - 1] - 1;

        if(NumItems[selection - 1] >= 0)
            Total_Price = Total_Price + ItemPrice[selection - 1];

        else{
            int error = 1;
            DisplayErrorMessage(error);         //Item finised
            cout <<selection<< endl;
        }
    }
    else if(selection == 10)
        cout << "\nTransaction Ended" << endl;

    else if(selection == 99){

        cout << "Enter the code to access maintanance: " <<endl;
        cin >> code;

        while(code != "111"){
            int error = 2;
            DisplayErrorMessage(error);
            cin >> code;
        }

        cout << endl;
        cout << "\t\t\t\t\tSales Report " << endl;
        cout << "==================================================== " << endl;
        cout << endl;
        cout << "Number of each product sold with Income cost: " << endl;
        cout << endl;

        do{
            if(NumItems[b] >= 0){

                Each_Item[b] = Each_Item[b] + ItemPrice[b];

                cout << NumItems[b] << "" << Product[b] << " sold for the total cost of " <<(10 - NumItems [b]) * Each_Item[b]/ 100 <<endl;   

                Total_Price = Total_Price + ((10 - NumItems[b]) * Each_Item[b]/100);
            }
            b++;
        }while(a <= ITEMS);
    }
    else{ 
        int error = 3;
        DisplayErrorMessage(error);
    }
}while(selection != 10);
}

DisplayErrorMessage.cpp

#include "Vending Machine.h"

void VendingMachine::DisplayErrorMessage(int error){

if (error == 1){
    cout << "\nSorry we are run out of item number ";
}
else if (error == 2){
    cout << "\nInvalid selection - Please re-select your choice" << endl;
}
else if (error == 3){
    cout << "\nIncorrect Password - Please re-enter" << endl;
}
else
    cout << "\nNot enough fund" << endl;
}

助けてください。

4

3 に答える 3

5

あなたの問題

extern int const ITEMS = 9;

これをマークすることexternは正しいのですが、あなたは先に進んで定義を与え、すばらしい正確さをすべて取り消してしまいました。

インクルード ガードを使用しているかどうかに関係なく、ヘッダーでシンボルを定義する場合、翻訳単位がリンクされていると、複数の定義エラーが発生します。

ITEMS正確に1 つの翻訳単位でシ​​ンボルを定義する必要があります。これは通常、ヘッダーではなく「ソース」ファイルで定義することを意味します。


素朴な修正

したがって、ヘッダーで:

extern int const ITEMS;  // definition found elsewhere

そして、1 つの.cpp ファイルで:

int const ITEMS = 9;

本当の修正

しかし、おっと!一部の配列サイズはそれに依存しているため、ヘッダーにこの定数が必要です。

したがって、ITEMSプログラム内のすべての翻訳単位で共有される 1 つのシンボルを作成する代わりに、翻訳単位にローカルな定義が必要になります。これを行うには、staticキーワードを使用してシンボルをファイル静的にします。

したがって、ヘッダーで:

static int const ITEMS = 9;  // visible only in this TU

これで、複数の翻訳単位 (大まかに言えば、このヘッダーを使用する各 .cpp を意味します) はITEMS、以前と同じように独自のバージョンの を持ちます... しかし、今回は、 とマークされているためstatic、それぞれがその翻訳単位に対してローカルです。そのため、リンク時に競合しません。

于 2013-08-20T15:15:11.517 に答える
3
  extern int const ITEMS = 9;

初期化子を含めるため、この宣言も定義です。そして、それを宣言するのでextern、オブジェクトは 1 つしかありませんITEMS。しかし、ヘッダー ファイルのその行を使用すると、ヘッダーを含むすべてのものが独自の定義になります。

を省略してもかまいません。externその場合、constは を意味しstaticます。

  int const ITEMS = 9;

次にITEMS、各コンパイル単位で異なるオブジェクトですが、常に値として使用し、そのアドレスで何もしない場合、それは大した問題ではありません。

または、C++11 サポートが有効になっている場合は、次のようにすることができます。

  constexpr int ITEMS = 9;

ITEMS...これは、1 つのオブジェクトを関数と同様に扱うようにコンパイラに指示し、inline複数回定義することができます。

于 2013-08-20T15:21:16.200 に答える