c++ - 在范围内声明 vector

标签 c++ vector

在我的 In-Progress BigInt 类中,我在 vector 声明方面遇到问题。我收到错误:

prog.cpp: In function 'void setBig1(std::string, int)': prog.cpp:45:3: error: 'big1' was not declared in this scope
big1.push_back(dig[x]);
^

prog.cpp: In function 'voidgetBig1(int)': prog.cpp:50:11: error: 'big1' was not declared in this scope
cout << big1[x] ;

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

//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

}

我非常感谢任何帮助。

最佳答案

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

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] ;
}

一般情况下,最好至少使用 size_t 或 std::vector::size_type 类型,而不是 int 类型

vector 本身可以使用私有(private)访问控制进行声明。

关于c++ - 在范围内声明 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29213382/

相关文章:

c# - 将枚举类型引用从 C# 传递到 C++/CLI 包装器到 C++

c++ - RTOS常用的设计模式(VXworks)

c++ - 使用Cmake输出库信息

c++ - vector 值的不同组合

c++ - 如何从充满 .txt 文件名的 vector 中选择随机元素 C++

c++ - 即使没有指定自定义比较器, pair<int,int> 的优先级队列如何工作?

c++ - 在 C++ 中排序然后打印 vector

c++ - 如何使用vc++找出当前线程堆栈上还剩下多少空间?

c++ - 从循环内的 vector 中删除项目

c++ - 在 C++ 中重命名(别名/转发)函数的最佳方法是什么?