C++ 错误 : identifier "barCode" is undefined

标签 c++ identifier

<分区>

在 ZipCode.cpp 的第 34 行,我的编译器(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++ 尤其陌生。任何帮助将不胜感激,谢谢。

最佳答案

像你定义的其他方法一样,它需要用它所在的类的名称来限定,所以

std::string ZipCode::getBarCode() {

关于C++ 错误 : identifier "barCode" is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135029/

相关文章:

css - CSS 标识符可以以两个连字符开头吗?

c++ - 从派生类返回值到基类

c++ - 科学 ofstream 中的指数只有 2 位数

c - 转换阶段 4 的非保留标识符是否会导致无法在转换阶段 7 保留文件范围标识符?

c++ - 编译错误: solution.c:20:5: error: expected identifier or ‘(’ before ‘{’ token {

javascript - 从javascript中的JSON子数组中检索数据,其中标识符以整数开头

c++ - 直接执行数据

c++ - Opencv特征描述给定坐标

c++ - 用于 Windows 开发的 GStreamer 或 DirectShow?

mysql - if else 在 mysql 查询中