-4

I'm making integer calculator using C++. Why is the following not working?

#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();
}

I intended in this way.

  1. First, "first integer" sentence appear on console. Type first integer what i want.
  2. Second, "second integer" sentence appear on console. Type second integer what i want.
  3. cuz of operator overloading, real1.i and real2.i added each other(like 1+2=3) and the result copy in lastum.i
  4. "result is (sum)"appears at last cuz of lastsum.show();

And here is the error message (I'm using onlinegdb but imma text only red sentences appeared.)

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);
      |     ~~~~~~~^~~~~~~~~~~~~~
  • 8
    You seem to have skipped some very important early chapters of your beginners text-books. Please don't do that. Or invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read from the beginning and not skipping anything. – Some programmer dude Aug 24 '23 at 14:18
  • 3
    None of your errors have anything to do with operator overloading, by the way. – Nathan Pierson Aug 24 '23 at 14:22
  • 2
    `Number` only has a non-default constructor. And `getline` is for strings, not numbers. – molbdnilo Aug 24 '23 at 14:23
  • Looks like you're rushing and trying to write too much code at a time. Write a few lines, just enough to accomplish one simple-and-easily-testable thing. Compile. Resolve all errors and warnings. Test to ensure you get exactly the desired behaviour, no more and no less. Then add just enough code to do one more thing and repeat. If you let mistakes build up the time you spend untangling them to resolve them will greatly exceed the time you saved by bulling through and writing everything at once. Writing code is the quick part of the job, so sacrifice time there to make debugging easier. – user4581301 Aug 24 '23 at 14:55
  • I can reduce your development time: Stop typing `this->` and access members and functions directly. You should develop a coding style where member names are different than parameter names. Less stuff to type means faster development. – Thomas Matthews Aug 24 '23 at 15:49

2 Answers2

2

There are a number of issues with the code's design, but putting those aside, and just fixing the code so it compiles and runs as expected...

#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();


}

The issues:

  1. Your original code getline(cin, real1.i) is reading into real1.i, but the member variable i is private. I made it public in the "fixed" version.
  2. In the Number::operator+() function you have the statement Number tmp with the intention of making a temporary Number object. However, the C++ doesn't know how to construct this object (it doesn't match your constructor). So, I added a default constructor for this. This fixes several errors.
  3. I also didn't like the readline() statement so I changed to cin >> x.

With those changes it should compile and work (did so with me, using g++ compiler on Linux).

There are a number of issues of course, like why you would do it this way in the first place, exposing i as public, etc. But I assume you are doing this just as way to experiment with C++ and operator overloading.

Try and get a good book or online course on C++.

0

Let me just do a quick refactor of your code,

#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;
    
}
  • thx a lot. But wdym 'now this does't work' in int main()part? I compile ur code and its working... Also I dont have idea how to 'Make constructor with no parameter and adding default value to extisting one'.. Can u show me code how to? Im not native sry... – Helpmeeeee Aug 28 '23 at 05:09
  • it works because i added a default value to the constructor `Number(int i = 0)`, the i=0 means that the default value is 0 – GreenChicken Aug 28 '23 at 08:54
  • i also think you skipped some very important lessons, that are not the topic of the question. Maybe you should do some in depth tutorial for C++ or find a good book for it – GreenChicken Aug 28 '23 at 08:57