【发布时间】: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 实例来访问它。你不能直接从课堂外访问它。