我正在使用 C++ 制作整数计算器。 为什么以下不起作用?
#include <iostream>
#include <string>
using namespace std;
class Number {
int i;
public:
Number(int i) {
this->i = i;
}
void show();
Number operator+(Number op2);
};
void Number::show() {
cout << "result is" << i << endl;
}
Number Number::operator+(Number op2) {
Number tmp;
tmp.i = this->i + op2.i;
return tmp;
}
int main() {
Number real1, real2, lastsum;
cout << "first integer" << endl;
getline(cin, real1.i);
cout << "second integer" << endl;
getline(cin, real2.i);
lastsum = real1 + real2;
lastsum.show();
}
我就是这么想的。
这是错误消息 (我正在使用 onlinegdb,但我的文本只出现红色句子。)
main.cpp:23:12:error: no matching function for call to ‘Number::Number()
23 | Number tmp;
| ^~~
main.cpp:32:12: error: no matching function for call to ‘Number::Number()’
32 | Number real1, real2, lastsum;
| ^~~~~
main.cpp:32:19: error: no matching function for call to ‘Number::Number()’
32 | Number real1, real2, lastsum;
| ^~~~~
main.cpp:35:24: error: ‘int Number::i’ is private within this context
35 | getline(cin, real1.i);
| ^
main.cpp:35:12: error: no matching function for call to ‘getline(std::istream&, int&)’
35 | getline(cin, real1.i);
| ~~~~~~~^~~~~~~~~~~~~~
main.cpp:38:24: error: ‘int Number::i’ is private within this context
38 | getline(cin, real2.i);
| ^
main.cpp:38:12: error: no matching function for call to ‘getline(std::istream&, int&)’
38 | getline(cin, real2.i);
| ~~~~~~~^~~~~~~~~~~~~~
代码的设计存在许多问题,但把这些问题放在一边,只需修复代码,使其按预期编译和运行......
#include <iostream>
#include <string>
using namespace std;
class Number{
public:
int i;
public:
Number() {
this->i = 0;
}
Number(int i) {
this->i=i;
}
void show();
Number operator+ (Number op2);
};
void Number::show(){
cout<<"result is "<<i<<endl;
}
Number Number::operator+(Number op2){
Number tmp;
tmp.i=this->i+op2.i;
return tmp;
}
int main(){
Number real1, real2, lastsum;
cout<<"first integer"<<endl;
cin >> real1.i;
cout<<"second integer"<<endl;
cin >> real2.i;
lastsum=real1+real2;
lastsum.show();
}
问题:
getline(cin, real1.i)
正在读入real1.i
,但成员变量i
是私有的。我在“固定”版本中公开了它。Number::operator+()
函数中,您有语句 Number tmp
,其目的是创建一个临时 Number 对象。然而,C++ 不知道如何构造这个对象(它与你的构造函数不匹配)。因此,我为此添加了一个默认构造函数。这修复了几个错误。cin >> x
。通过这些更改,它应该可以编译并运行(我就是这么做的,在 Linux 上使用 g++ 编译器)。
当然有很多问题,比如为什么你首先要这样做,将
i
公开为公共,等等。但我认为你这样做只是为了尝试 C++ 和运算符重载。
尝试获取一本有关 C++ 的好书或在线课程。
让我快速重构一下您的代码,
#include <iostream>
#include <string>
// FIRST: Stop using namespace, especially namespace std
// using namespace std;
class Number{
int i; // this is private, so you get a getter and setter for it
public:
// this is a constructor
Number(int i = 0) { // i added the '=0' so it can simulate a constructor with no parameters
this->i=i;
}
void show();
Number operator+(Number op2);
// you need a setter for the private atribute i
void setI(int number) {
this->i = number;
}
// you need a getter for your private atribute i
int getI() {
return i;
}
};
void Number::show(){
std::cout<<"result is "<<i<<std::endl;
}
Number Number::operator+(Number op2){
// get the values of i from getters, insted of this->i and op2.i
return Number(getI() + op2.getI());
// this is not necesary now
// Number tmp;
// tmp.i=this->i+op2.i;
// return tmp;
}
int main(){
// okay, now this does't work because you dont have a constructor with no parameters
// you either have to add a default value to the existing one
// or make a new with no parameters
Number real1, real2, lastsum;
std::cout<<"first integer"<<std::endl;
// dont do this, just... why
// getline(cin, real1.i);
// do this instead
int i;
std::cin>>i;
real1.setI(i);
// same for real2, you can use the same variable i
std::cout<<"second integer"<<std::endl;
std::cin>>i;
real2.setI(i);
lastsum=real1+real2;
lastsum.show();
// always add return 0; at the end of yout main() funtion
return 0;
}