-2

I need to create a c++ program that implements sum= num1 + num2 , sum= num1 - num2, sum= num1 * num2, where sum,num1,num2 are objects containing vector that stores an integer. I need to use operator overloading for this. This is the code that I came up with but I am not able to understand the reason for these errors. I declared the vector as 'std::vector n ;' and tried to pass another vector as argument to the function. I am new to both stack overflow and C++ programming.

#include<iostream>
using namespace std;
 
class NUM
{
    private:
        std::vector<int> n ;
         
    public:
        //function to get number
        void getNum(std::vector<int> x)
        {
            n=x;
        }
        //function to display number
        void dispNum(void)
        {
            cout << "Number is: " << n;
        }
        //add two objects - Binary Plus(+) Operator Overloading
        NUM operator +(NUM &obj)
        {
            NUM x;  //create another object
            x.n=this->n + obj.n;
            return (x); //return object
        }
};
int main()
{
    NUM num1,num2,sum;
    num1.getNum(10);
    num2.getNum(20);
     
    //add two objects
    sum=num1+num2;
     
    sum.dispNum();
    cout << endl;
    return 0;
} 

These are the errors that I'm getting.

main.cpp:7:14: error: ‘vector’ in namespace ‘std’ does not name a template type
         std::vector<int> n ;
              ^~~~~~
main.cpp:11:26: error: ‘std::vector’ has not been declared
         void getNum(std::vector<int> x)
                          ^~~~~~
main.cpp:11:32: error: expected ‘,’ or ‘...’ before ‘<’ token
         void getNum(std::vector<int> x)
                                ^
main.cpp: In member function ‘void NUM::getNum(int)’:
main.cpp:13:13: error: ‘n’ was not declared in this scope
             n=x;
             ^
main.cpp:13:15: error: ‘x’ was not declared in this scope
             n=x;
               ^
main.cpp: In member function ‘void NUM::dispNum()’:

main.cpp:18:38: error: ‘n’ was not declared in this scope
             cout << "Number is: " << n;
                                      ^
main.cpp: In member function ‘NUM NUM::operator+(NUM&)’:

main.cpp:24:15: error: ‘class NUM’ has no member named ‘n’
             x.n=this->n + obj.n;
               ^
main.cpp:24:23: error: ‘class NUM’ has no member named ‘n’
             x.n=this->n + obj.n;
                       ^
main.cpp:24:31: error: ‘class NUM’ has no member named ‘n’
             x.n=this->n + obj.n;
  • 6
    You're missing at least `#include `, this fixes a lot of errors. And `getNum` looks weird. I don't think it does what you think it does, furthermore you're expecting a vector but passing a number. – pschulz Jan 19 '21 at 11:26
  • 3
    I would recommend a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to at least get familiar with the basic concepts. – Quimby Jan 19 '21 at 11:32

2 Answers2

0
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;

class NUM
{
    private:
    std::vector<int> n ;

    public:
        //function to get number
        void getNum(std::vector<int> x)
        {
            n=x;
        }
        //function to display number
        void dispNum(void)
        {
            cout << "Number is: " <<endl;
            for_each(n.begin(),n.end(),[](int i){cout <<i<<endl;});
        }
        //add two objects - Binary Plus(+) Operator Overloading
        NUM operator +(NUM &obj)
        {
            NUM x;  //create another object
            //x.n=this->n + obj.n;
            if(this->n.size() == obj.n.size())
                for (int i = 0; i < this->n.size(); ++i) {
                    x.n.push_back(this->n.at(i)+obj.n.at(i));
                }


            return x; //return object
        }

};
int main()
{
    NUM num1,num2,sum;
    num1.getNum({10});
    num2.getNum({20});

    //add two objects
    sum=num1+num2;

   sum.dispNum();

    cout << endl;
    return 0;
}
Ruslan
  • 16
  • 1
-1

You haven't included the vector header, which is causing the error

#include <iostream>
#include <vector>
using namespace std;

This will solve the error.
Or You can instead include the header

#include <bits/stdc++.h>
using namespace std;

This will include the whole standard library at once.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 6
    Neither including anything from `` directly nor including "the whole standard library" is a good idea. – zkoza Jan 19 '21 at 13:17