链接错误:找到一个或多个多重定义的符号
Linking error: One or more multiply defined symbols found
我试图编译我的程序,但我遇到了这些错误,我不知道在哪里可以找到并更正它。
1>Main.obj : error LNK2005: "int * Coins" (?Coins@@3PAHA) already defined in DisplayErrorMessage.obj
1>Main.obj : error LNK2005: "int * NumCoins" (?NumCoins@@3PAHA) already defined in DisplayErrorMessage.obj
1>Main.obj : error LNK2005: "int * ItemPrice" (?ItemPrice@@3PAHA) already defined in DisplayErrorMessage.obj
1>Main.obj : error LNK2005: "int * NumItems" (?NumItems@@3PAHA) already defined in DisplayErrorMessage.obj
1>MakeSelection.obj : error LNK2005: "int * Coins" (?Coins@@3PAHA) already defined in DisplayErrorMessage.obj
1>MakeSelection.obj : error LNK2005: "int * NumCoins" (?NumCoins@@3PAHA) already defined in DisplayErrorMessage.obj
1>MakeSelection.obj : error LNK2005: "int * ItemPrice" (?ItemPrice@@3PAHA) already defined in DisplayErrorMessage.obj
1>MakeSelection.obj : error LNK2005: "int * NumItems" (?NumItems@@3PAHA) already defined in DisplayErrorMessage.obj
1>ShowMenu.obj : error LNK2005: "int * Coins" (?Coins@@3PAHA) already defined in DisplayErrorMessage.obj
1>ShowMenu.obj : error LNK2005: "int * NumCoins" (?NumCoins@@3PAHA) already defined in DisplayErrorMessage.obj
1>ShowMenu.obj : error LNK2005: "int * ItemPrice" (?ItemPrice@@3PAHA) already defined in DisplayErrorMessage.obj
1>ShowMenu.obj : error LNK2005: "int * NumItems" (?NumItems@@3PAHA) already defined in DisplayErrorMessage.obj
1>c:userskanaandocumentsvisual studio 2010ProjectsAssign2DebugAssign2.exe : fatal error LNK1169: one or more multiply defined symbols found
这是完整的代码:
自动售货机.h
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int Denominations = 5;
int const ITEMS = 9;
class VendingMachine {
public:
void MakeSelection(int NumItems [], int ItemPrice[]);
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
};
MakeSelection.cpp
#include "VendingMachine.h"
void VendingMachine::MakeSelection(int NumItems [], int ItemPrice[]) {
int Coins[] = {100, 50, 20, 10, 5};
int NumCoins[] = {10, 10, 10, 10, 10}; //assume we have 10 coins of each denomination
int ItemPrice[ ] = { 75, 120, 120, 100, 150, 95, 110, 50, 120 }; //price in cents
int NumItems[ ] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };
double Total_Price = 0;
double Item_Total = 0;
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 << "tttttSales 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);
}
您可以使用cpp文件和标题
头中的int * Coins;
不是定义全局的方式。这是一个定义。你应该使用
extern int* Coins;
并在单个转换单元(cpp文件)中定义变量。
因为您在所有3个cpp文件中都包含了带有定义的标头,所以该符号会被定义多次,从而导致错误。
在头文件中使用包含保护:
#ifndef _VENDING_MACHINE_H_
#define _VENDING_MACHINE_H_
//your VendingMachine.h contents
#endif //_VENDING_MACHINE_H_
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用命名空间时出现多个定义错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 在运算符重载定义中使用成员函数(const错误)
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 已定义函数时出现 G++ "未定义的引用"错误
- C++ G++ 编译器 - 错误:隐式声明的定义
- 链接 cmake 时出现未定义的引用错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接阶段出现多重定义错误
- 已经以性格错误定义了
- vim ctag导致了对乐趣的错误定义
- C++ 隐式声明的编译器错误定义
- 编译器错误-定义问题.C++