我的编译器(MS Visual Studios 2013)给我错误:标识符“barCode”未定义。这是我的头文件ZipCode.h:
#ifndef _ZIPCODE_H
#define _ZIPCODE_H
#include <string>
class ZipCode {
private:
std::string barCode;//The Bar Code
void convDigit(int);//Converts a single digit to its bar code equivalent
public:
ZipCode(int);//Constructor recieving a zip code
ZipCode(std::string);//Constructor recieving a bar code
int getZipCode(void);//Returns the zip code
std::string getBarCode(void);//Returns the bar code
};
#endif
源代码ZipCode.cpp:
#include "ZipCode.h"
#include <string>
ZipCode::ZipCode(std::string a){
barCode = a;
}
ZipCode::ZipCode(int a){
barCode = "";
for (int b = 0; b < 5; b++){
int digit = a % 10;
a /= 10;
convDigit(digit);
}
}
void ZipCode::convDigit(int a){
switch (a){
case 0: barCode = std::string("11000") + barCode; break;
case 1: barCode = std::string("00011") + barCode; break;
case 2: barCode = std::string("00101") + barCode; break;
case 3: barCode = std::string("00110") + barCode; break;
case 4: barCode = std::string("01001") + barCode; break;
case 5: barCode = std::string("01010") + barCode; break;
case 6: barCode = std::string("01100") + barCode; break;
case 7: barCode = std::string("10001") + barCode; break;
case 8: barCode = std::string("10010") + barCode; break;
case 9: barCode = std::string("10100") + barCode; break;
}
}
std::string getBarCode(){
return (barCode);//Error: identifier "barCode" is undefined
}
在前三个函数中我使用barCode并且没有问题,所以我很困惑为什么getBarCode()现在有问题。我试过了:
return (ZipCode::barCode)//Error: member ZipCode::barCode is inaccessible.
return (this->barCode)//Error: 'this' may only be used inside a non-static member function.
上述两个错误也让我感到困惑,因为barCode和getBarCode都是ZipCode类的成员,而getBarCode不是静态函数。我还是编程新手(如果不是很明显),我对C ++也是新手。非常感谢任何帮助,谢谢。
答案 0 :(得分:2)
与您定义的其他方法一样,它需要使用其所在类的名称进行限定,因此
std::string ZipCode::getBarCode() {