我在名为Phone.h
的头文件中创建了我的类,现在我有一个PhoneC.cpp
,我在Phone.h
然后我的main.cpp
定义了我的函数。我声明int main等等。虽然我收到错误
one or more multiply defined symbols found
我不太清楚这意味着什么,也没有任何其他帖子解释这个解决方案是什么明显的。
Phone.h
#ifndef PHONE_IMEI_API
#define PHONE_IMEI_API
#include <string>
class Phone {
public:
bool checkIMEI(std::string IMEI);
void sort();
std::string data[5] = {};
} Phone;
#endif
PhoneC.cpp
#include "Phone.h";
bool Phone::checkIMEI(std::string IMEI)
{
int IMEIlength = 15;
int len, i, mul, sum, m10;
len = IMEI.length();
char chksum = IMEI[len];
i = 0;
if (len != IMEIlength)
return false;
mul = 2; sum = 0;
for (i = len - 2; i >= 0; i--)
{
int currentNumber = static_cast<int>(IMEI[len]);
if (currentNumber * mul >= 10)
sum += ((currentNumber * mul) / 10) + ((currentNumber * mul) % 10);
else
sum += currentNumber * mul;
if (mul == 2)
mul = 1;
else
mul = 2;
}
m10 = sum % 10;
if (m10)
m10 = 10 - m10;
if (m10 == chksum)
return true;
else
return false;
}
的main.cpp
#include <iostream>
#include <iomanip>
#include "Phone.h"
using namespace std;
int main(){
string imei;
cout << "Please enter an IMEI" << endl;
cin >> imei;
if (Phone.checkIMEI(imei))
{
cout << "Hello World";
}
return 0;
}
不确定我做错了什么我尝试遵循this official documentation的文档仍然没有运气。感谢任何明显的建议,不完整的C ++课程
答案 0 :(得分:0)
与类和对象名称相同的Phone
是错误的。所以我做的是
#ifndef PHONE_IMEI_API
#define PHONE_IMEI_API
#include <string>
class Phone {
public:
bool checkIMEI(std::string IMEI);
void sort();
std::string data[5] = {};
};
#endif
<强> PhoneC.cpp 强>
#include "Phone.h";
bool Phone::checkIMEI(std::string IMEI)
{
int IMEIlength = 15;
int len, i, mul, sum, m10;
len = IMEI.length();
char chksum = IMEI[len];
i = 0;
if (len != IMEIlength)
return false;
mul = 2; sum = 0;
for (i = len - 2; i >= 0; i--)
{
int currentNumber = static_cast<int>(IMEI[len]);
if (currentNumber * mul >= 10)
sum += ((currentNumber * mul) / 10) + ((currentNumber * mul) % 10);
else
sum += currentNumber * mul;
if (mul == 2)
mul = 1;
else
mul = 2;
}
m10 = sum % 10;
if (m10)
m10 = 10 - m10;
if (m10 == chksum)
return true;
else
return false;
}
<强>的main.cpp 强>
#include <iostream>
#include <iomanip>
#include "Phone.h"
using namespace std;
int main(){
Phone phone;
string imei;
cout << "Please enter an IMEI" << endl;
cin >> imei;
if (phone.checkIMEI(imei))
{
cout << "Hello World";
}
return 0;
}
这似乎有效。如果有任何建议,为什么和我做错了什么。请发布答案。或者我只是将其标记为正确。 -_-