【问题标题】:declaring vectors in scope在范围内声明向量
【发布时间】:2015-05-26 15:04:16
【问题描述】:

在我的 In-Progress BigInt 类中,我在声明向量时遇到了问题。我收到错误消息:

prog.cpp:在函数 'void setBig1(std::string, int)' 中: prog.cpp:45:3: 错误: 'big1' 未在此范围内声明
big1.push_back(dig[x]);
^

prog.cpp:在函数 'voidgetBig1(int)' 中: prog.cpp:50:11: 错误: 'big1' 未在此范围内声明
cout

我相信我与向量 big1 相关的 getter 和 setter 没有识别类定义的“public:”部分中向量的 decleration。但我找不到错误的解决方案或明确的原因。我的代码如下:

//my bigInt class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//class, constructors
//overload operators methods
//add, subtract, multiply, divide
class bigInt{//class
    public:
        bigInt();
        ~bigInt();
        void setString(string dig);
        string getString(void);
        int getDigitLength(void);
        std::string digit;
        int digitLength;
        std::string digString;
        void setBig1(string dig, int dLength);
        std::vector<int> big1;
        std::vector<int> big2;
        void getBig1(int dLength);
};
//constructors
bigInt::bigInt(void){
    std::vector<int> big1;
}
//deconstructor
bigInt::~bigInt(){

}
//getters/setters
void bigInt::setString(string dig){
    digit= dig;
    digitLength= (digit.length());
}
string bigInt::getString(){
    return digit;
}
int bigInt::getDigitLength(){
    return digitLength;
}
void setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

}

int main(){
    string digString= "1"; //string
    bigInt my_int{};
    //bigInt big1<int>;
    my_int.setString(digString); //setInternalString to equal digString
    cout << my_int.getString() << endl; //prints internal string
    my_int.setBig1(my_int.getString(), my_int.getDigitLength());//sets big1 vector = to string
    my_int.getBig1(my_int.getDigitLength()); //print big1 vector

}

非常感谢任何帮助。

【问题讨论】:

  • 在成员函数定义中缺少bigInt::
  • big1 是 BigInt 的成员。您仍然需要一个 bigint 实例来访问它。你不能直接从课堂外访问它。

标签: c++ vector


【解决方案1】:

您忘记指定定义成员函数的类。而不是

void setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

void bigInt::setBig1(string dig, int dLength){
    for(int x= 0; x<(dLength); x++)     
    {
        big1.push_back(dig[x]);
    }
}
void bigInt::getBig1(int dLength){
    for(int x= 0; x<(dLength); x++){
        cout << big1[x] ;
    }

您还可以使用限定符 const 声明所有 getter,因为它们不会更改类的对象本身。例如

class bigInt
{
//...

    void getBig1(int dLength) const;
};

void bigInt::getBig1(int dLength) const
{
    for ( int i = 0; i < dLength; i++ ) cout << big1[i] ;
}

一般情况下,最好使用至少 type size_t 或 std::vector::size_type 来代替 int 类型

向量本身可以用访问控制私有来声明。

【讨论】:

  • 加一个用于添加const后缀。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 2016-09-18
  • 2017-02-12
相关资源
最近更新 更多