Like the other methods you define, it needs to be qualified with the name of the class its in, so
std::string ZipCode::getBarCode() {
Pergunta
on line 34 of ZipCode.cpp my compiler (MS Visual Studios 2013) is giving me Error: identifier "barCode" is undefined. Here is my header file 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
Source code 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
}
In the previous three functions I use barCode and there is no problem there, so I'm confused why there is now a problem in getBarCode(). I've tried:
return (ZipCode::barCode)//Error: member ZipCode::barCode is inaccessible.
return (this->barCode)//Error: 'this' may only be used inside a non-static member function.
Both of the above errors also confuse me, because barCode and getBarCode are both members of the ZipCode class, and getBarCode is not a static function. I'm still new to programming (if it isn't obvious) and I'm especially new to C++. Any help would be greatly appreciated, thank you.
Solução
Like the other methods you define, it needs to be qualified with the name of the class its in, so
std::string ZipCode::getBarCode() {