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;