confused scope problem
Jan 1, 2019 at 8:59am UTC
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;
class string_number{
public :
string_number();
string_number(string);
~string_number();
const string& value() const ;
string_number& operator =(const string_number&);
string_number operator +(const string_number&);
string_number operator *(const string_number&);
private :
string value_one;
};
ostream& operator <<(ostream&, const string_number&);
istream& operator >>(istream&, const string_number&);
int main(){
string_number a("123" );
string_number b("2" );
a = a * b;
cout << a.value();
return 0;
}
string_number::string_number(){
}
string_number::string_number(string one){
value_one = one;
}
string_number::~string_number(){
}
const string& string_number::value() const {
return value_one;
}
string_number& string_number::operator =(const string_number& one){
if (this != &one){
value_one.clear();
value_one = one.value();
}
return *this ;
}
string_number string_number::operator +(const string_number& one){
stringstream two;
string result;
char fill;
int add_number = 1;
int number_plus = 0;
int digit = 0;
int digit_location = 0;
if (value_one.length() >= one.value().length()){
digit = value_one.length();
}else {
digit = one.value().length();
}
while (digit_location < digit || add_number != 0){
if (digit_location == 0){
add_number = 0;
}
if (digit_location < one.value().length()){
two << one.value()[one.value().length()-1-digit_location];
two >> number_plus;
two.clear();
}
add_number = add_number + number_plus;
number_plus = 0;
if (digit_location < value_one.length()){
two << value_one[value_one.length()-1-digit_location];
two >> number_plus;
two.clear();
}
add_number = add_number + number_plus; //已完成兩位數相加
number_plus = 0;
two << add_number % 10;
two >> fill;
two.clear();
result.insert(result.begin()+0, fill);
add_number = add_number / 10;
digit_location ++;
}
string_number result_number(result);
return result_number;
}
string_number string_number::operator *(const string_number& one){
string_number result("0" );
for (int i = one.value().length() - 1; i > -1; i--){
string_number one_iterate;
string for_iterate;
int for_one = 0;
stringstream temp;
temp << one.value()[i];
temp >> for_one;
temp.clear();
int add_number = 0;
for (int j = value_one.length() - 1; j > -1; j--){
int for_this = 0;
char char_digit;
temp << value_one[j];
temp >> for_this;
temp.clear();
add_number = add_number + (for_one * for_this);
temp << (add_number % 10);
temp >> char_digit;
for_iterate.insert(for_iterate.begin()+0, char_digit);
temp.clear();
add_number = add_number / 10;
}
if (i != one.value().length() - 1){
for_iterate.append(1, '0' );
}
one_iterate = string_number(for_iterate);
result = result + one_iterate;
}
return result;
}
ostream& operator <<(ostream& one, string_number& two){
one << two.value();
return one;
}
istream& operator >>(istream& one, string_number& two){
string consturct;
one >> construct;
two = string_number(construct);
return one;
}
When i compile, compiler show the error message:
error: ‘construct’ was not declared in this scope
I felt so confused, since I declared
string construct
in function
operator >>
. what is the not declared
construct
mean?
Please tell me what happened in this code.
Last edited on Jan 1, 2019 at 9:01am UTC
Jan 1, 2019 at 9:04am UTC
1 2
string consturct;
one >> construct;
Check your spelling!
Jan 1, 2019 at 9:06am UTC
omg i felt so shameful. i have the urge to just delete it.
Topic archived. No new replies allowed.