I am working on an assignment where I need to overload the +, -, ==, =!, >> and << operators using a Complex number class. I have the + and - working, but when I try to compile with my == and =! I am getting errors. can someone tell me what I have wrong?
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex
#endif
these are the errors returned by the compiler
error C2143: syntax error : missing ';' before '!'
error C2460: 'Complex::=' : uses 'Complex', which is being defined
see declaration of 'Complex'
error C2059: syntax error : '('
error C2238: unexpected token(s) preceding ';'
error C2804: binary 'operator <<' has too many parameters
error C2804: binary 'operator >>' has too many parameters
error C2365: 'Complex::=' : redefinition; previous definition was 'data member'
see declaration of 'Complex::='
error C2556: 'bool Complex::operator ==(const Complex &) const' : overloaded function differs only by return type from 'Complex Complex::operator ==(const Complex &) const'
see declaration of 'Complex::operator =='
error C2371: 'Complex::operator ==' : redefinition; different basic types
see declaration of 'Complex::operator =='
error C2143: syntax error : missing ';' before '!'
error C2761: 'int Complex::=' : member function redeclaration not allowed
error C2059: syntax error : '('
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before '!'
error C2460: 'Complex::=' : uses 'Complex', which is being defined
1> d:\my documents\visual studio 2005\projects\week 11\week 11\complex1.h(6) : see declaration of 'Complex'
error C2059: syntax error : '('
error C2238: unexpected token(s) preceding ';'
error C2804: binary 'operator <<' has too many parameters
error C2804: binary 'operator >>' has too many parameters
error C2365: 'Complex::=' : redefinition; previous definition was 'data member'
see declaration of 'Complex::='
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
Okay here is myheader now, when you say they only take 1 operator I am confused as to what you mean. Here are my errors...
error C2804: binary 'operator <<' has too many parameters
error C2804: binary 'operator >>' has too many parameters
error C2106: '=' : left operand must be l-value
error C2166: l-value specifies const object
error C2804: binary 'operator <<' has too many parameters
error C2804: binary 'operator >>' has too many parameters
Here are some additional notes...I'll look at the stream operators while you look at this
Main.cpp
Need to match signature (returns bool)
bool Complex::operator!=( const Complex &operand2 ) const
-----------------------------
Wrong operator (should be != instead of =!)
bool Complex::operator!=( const Complex &operand2 ) const
{
if (real != operand2.real || imaginary != operand2.imaginary)
return true;
}
----------------------------
Need the "else" branch for these
bool Complex::operator==( const Complex &operand2 ) const
{
if (real == operand2.real && imaginary == operand2.imaginary)
return true;
else
return false;
}
bool Complex::operator!=( const Complex &operand2 ) const
{
if (real != operand2.real || imaginary != operand2.imaginary)
return true;
else
return false;
}
Need accessors and modifiers for private real and imaginary parts in complex1.h
double Real() { return real; }
double Imaginary() { return imaginary; }
void Real(double r) { real = r; }
void Imaginary(double i) { imaginary = i; }
0
urobinsAuthor Commented:
Here are my errors now.
complex1.cpp(35) : error C2270: '<<' : modifiers not allowed on nonmember functions
complex1.cpp(35) : error C2805: binary 'operator <<' has too few parameters
complex1.cpp(39) : error C2270: '>>' : modifiers not allowed on nonmember functions
complex1.cpp(39) : error C2805: binary 'operator >>' has too few parameters
complex1.cpp(50) : error C2106: '=' : left operand must be l-value
complex1.cpp(50) : error C2166: l-value specifies const object
Now I would like to explain anything that is confusing to you.
What questions do you have?
0
urobinsAuthor Commented:
thanks I'll have a look at that.
0
urobinsAuthor Commented:
I'm still getting some compliation errors. What are the accessors you mentioned for? I hadn't heard of using those before? do they go in public or private? Sorry to ask so many questions I am really trying to grasp this assignment.
"Accessors" is a term used for public methods that return (or set) private values. The "Real" functions in this header are accessors. Some people like to use "Accessor" and "Modifier", where Accessors return values and Modifiers set them.
Accessor and Modifier functions are public, so that the caller can access them. They allow public access to data that would otherwise be private. The idea is that by providing accessors and modifiers, you retain control over your private data. You could, for example, require the caller to provide an access code and have the Accessor or Modifier check that code. That isn't common, I'm just using that as an example.
Operator is !=, not =!
Complex operator!=( const Complex &operand2 ) const;
These operators take one arg
std::ostream& operator<<(const Complex&)const;
std::istream& operator>>(const Complex&)const;