overload operator+ in format (z + obj2)
I need help to overload the operator + in the format (z + obj2).
I have done it in two formats where 'n' becomes the sum of two objects and 'z' is z (scalar variable/constant/expression).
1. n = obj2 + obj 1 (done)
---> declaration
double operator+(const product&);
----> definition
// Overloading the operator+ (productObj2 + productObj1)
double product::operator +(const product & rhs)
{
return (*this).productCost + rhs.productCost ;
}
2. n = obj2 + z (scalar variable/constant/expression) (done)
---> declaration
double operator+(const double&)----> definition
----> definition
double product::operator +(const double & rhs)
{
cout << "The operator+ (productObj2 + z) function ran." << endl;
return (*this).productCost + rhs ;
}
3. n = z + obj2 <---------- Need help/tips/comments to overload operator +
<Need help/tips/comments to overload operator + in the above format>
Thanks.
Need to overload opertaor+
Thanks zx-1, for you reply. But I am still having the issue.
Declaration-->
friend double operator+(const double&, const product &);
Definition-->
double product:: operator +(const double & num, const product & rhs )
{
......
}
But I am still getting the following error:
error C2511: '+' : overloaded member function 'double (const double &,const class product &)' not found in 'product'
I am not sure why compiler is still complaining?
Quote:
Originally Posted by zx-1
Make it a friend to the class, with the signature operator+(const double &, const obj &)
Thanks for the tips/help...
ChaosEngine/Decrypt/Imalc,
I really appreciate your help and tips. My code works now.
Thanks.
Next hic-up in overloading the extraction (>>)operator
The overloading of extraction operator(>>) is giving the following errors. The insertion (<<)operator is working within the code. It looks like there are some header files missing to be included? or there are other errors which I can't see.
Code:
Declaration:
#include <iostream>
.
.
class x{
friend ostream &operator<<(ostream &, const product &); <--- Work fine
friend istream &operator>>(istream &, product &); <---- Giving the errors shown below
public:
}
Definition:
istream &operator >>(ostream &input, product &p)
{
.........
}
Compiler Error:
------------------Configuration: useProduct - Win32 Debug--------------------
Compiling...
product.cpp
error C2143: syntax error : missing ';' before '&'
error C2433: 'istream' : 'friend' not permitted on data declarations
error C2501: 'istream' : missing storage-class or type specifiers
error C2244: 'istream' : unable to resolve function overload
error C2061: syntax error : identifier 'istream'
error C2501: '>>' : missing storage-class or type specifiers
error C2805: binary 'operator >>' has too few parameters
Error executing cl.exe.
product.obj - 7 error(s), 0 warning(s)
Thanks.
Code comiples on Unix (Solaris) but not on Visual C++
Code comiples on Unix (Solaris) but not on Visual C++? Any suggestions....
--------------------Configuration: useProduct - Win32 Debug--------------------
Compiling...
product.cpp
c:\scc\assign 2\product.h(39) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
product.obj - 1 error(s), 0 warning(s)