Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created May 1, 2011 17:05
Show Gist options
  • Save lazypower/950649 to your computer and use it in GitHub Desktop.
Save lazypower/950649 to your computer and use it in GitHub Desktop.
//Complex.cpp
#include <iostream>
#include "Complex.h"
using std::cout;
using std::endl;
/**
* Creates a new Complex with default values for real and imaginary
* of zero.
*/
Complex::Complex()
{
setReal(0);
setImaginary(0);
}
/**
* Creates a new Complex with user-specified values.
*/
Complex::Complex(double real, double imaginary)
{
setReal(real);
setImaginary (imaginary);
}
/**
* Default destructor of Complex.
*/
Complex::~Complex()
{
}
/**
* Return the value of the real portion of the complex number.
*/
double Complex::getReal() const
{
return real;
}
/**
* Return the value of the imaginary portion of the complex number.
*/
double Complex::getImaginary() const
{
return imaginary;
}
/**
* Set the value of the real portion to a new value.
*/
void Complex::setReal(double real)
{
this->real = real;
}
/**
* Set the value of the imaginary portion to a new value.
*/
void Complex::setImaginary(double imaginary)
{
this->imaginary = imaginary;
}
/**
* Adds two Complex numbers together and returns the sum.
*/
Complex Complex::add(const Complex & right) const
{
Complex sum(getReal() + right.getReal(),
getImaginary() + right.getImaginary());
return sum;
}
/**
* Adds two Complex numbers together and returns the sum.
*/
Complex Complex::operator+(const Complex & right) const
{
cout<<"Complex::operator+(const Complex & right) const is called"<<endl;
Complex sum(getReal() + right.getReal(),
getImaginary() + right.getImaginary());
return sum;
}
/**
* Adds a real number and a Complex numbers together and returns the sum.
*/
Complex Complex::operator+(double realNumber) const
{
cout<<"Complex::operator+(double realNumber) const is called"<<endl;
Complex sum(getReal() + realNumber, getImaginary());
return sum;
}
/**
* The global function performs complexObject3 = realNumber + complexObject1
*/
Complex operator+(double realNumber, Complex & right)
{
cout<<"operator+(double realNumber, const Complex & right) is called"<<endl;
Complex sum(realNumber+right.getReal(), right.getImaginary());
return sum;
}
/**
* The overloading +=
*/
void Complex::operator+=(Complex & right)
{
cout<<"Complex::operator+=(const Complex & right) is called"<<endl;
setReal(getReal( )+right.getReal());
setImaginary(getImaginary()+right.getImaginary());
}
/**
* Subtracts two Complex numbers together and returns the result.
*/
Complex Complex::subtract(const Complex & right) const
{
return Complex (getReal() - right.getReal(),
getImaginary() - right.getImaginary());
}
/**
* Subtracts two Complex numbers together and returns the result.
*/
Complex Complex::operator-(const Complex & right) const
{
cout<<"Complex::operator-(const Complex & right) const is called"<<endl;
return Complex (getReal() - right.getReal(),
getImaginary() - right.getImaginary());
}
/**
* copy a complex number.
*/
void Complex::copy(const Complex & right)
{
setReal(right.getReal());
setImaginary(right.getImaginary());
}
/**
* copy a complex number.
*/
void Complex::operator=(const Complex & right)
{
cout<<"Complex::operator=(const Complex & right) is called"<<endl;
setReal(right.getReal());
setImaginary(right.getImaginary());
}
void Complex::operator=(const double h)
{
setReal(h);
setImaginary(0);
}
/**
* Compare two complex numbers
*/
bool Complex::operator== (Complex & right) const
{
cout<<"Complex::operator==(const Complex & right) const is called"<<endl;
if (getReal() == right.getReal() &&
getImaginary() == right.getImaginary())
return true;
else
return false;
}
/**
* Prints the complex number in the form (real, imaginary)
*/
void Complex::print()
{
cout << "(" << getReal() << ", " << getImaginary() << ")" << endl;
}
/**
* Overloading input stream operator
* Complex class could perform I/O like built-in type.
* The format is "(real, imaginary)"
*/
istream& operator>>(istream & input, Complex & cNumber)
{
double real, imaginary;
input.ignore(256, '('); //ignor (
input>>real; //read real part
input.ignore(256,','); //white spaces up to next "," are ignored.
input>>imaginary; //read imaginary part
input.ignore(256,')'); //ignore white spaces after it.
if (input.good())
{
cNumber.real = real;
cNumber.imaginary = imaginary;
}
return input; // enable cin >> c1 >> c2 >> c3
}
/**
* Overloading input stream operator
* Complex class could perform I/O like built-in type.
* The format is "(real, imaginary)"
*/
ostream& operator<<(ostream & output, const Complex & cNumber)
{
output<<"(" <<cNumber.real<<", "<<cNumber.imaginary<<")";
return output; //enable cout << c1 << c2 << c3
}
//Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::istream;
using std::ostream;
class Complex
{
friend istream & operator>>(istream &, Complex &); //overloading input stream operator
friend ostream & operator<<(ostream &, const Complex &); //overloading output stream operator
public:
Complex(); //default constructor
Complex(double, double); //constructor
~Complex();//destructor
//adding two complex numbers
Complex add(const Complex &) const;
Complex operator+(const Complex &) const ;
//adding a realnumber and a complex number
Complex operator+(double) const;
//adding two complex number and update the original complex number
void operator+=(Complex &);
//subtracting two complex numbers
Complex subtract(const Complex &) const;
Complex operator-(const Complex &) const;
//copy a complex number
void copy(const Complex &);
void operator=(const Complex &);
void operator=(const double);
//compare two complex numbers
bool operator==(Complex &) const;
void print(); //print complex number format
double getReal() const;//getters
double getImaginary() const;
void setReal(double); //setters
void setImaginary(double);
private:
double real;
double imaginary;
}; //end of class
Complex operator+(double realNumber, Complex & right) ;
#endif
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex a( 1, 7), b(0, 2), c;
a.print();
cout << " + ";
b.print();
cout << " = ";
//cout = a.add( b );
//c.printComplex();
}
@dimka11
Copy link

dimka11 commented Oct 27, 2017

Its not work. Multiple errors.
Severity Code Description Project File Line Suppression State
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 22
Error (active) E0898 nonmember operator requires a parameter with class or enum type Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0386 no instance of overloaded function "real" matches the required type Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 40
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 13
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 31
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 38
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 46
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 54
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 62
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0020 identifier "setReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 15
Error (active) E0020 identifier "setReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 24
Error (active) E0020 identifier "setImaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 16
Error (active) E0020 identifier "setImaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 25
Error (active) E0020 identifier "right" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0020 identifier "imaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 48
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 111
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0065 expected a ';' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 121
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 111
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 38
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 46
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0258 'this' may only be used inside a nonstatic member function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 56
Error (active) E0258 'this' may only be used inside a nonstatic member function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 64

@shyamzzp
Copy link

You can put everything into one file "driver.cpp" that resolved the error for me.
i made a public gist for the same : https://gist.github.com/shyamzzp/ff8edb3a6ffd7f19b5a4e97d69fb12bd

@ch1y0q
Copy link

ch1y0q commented Feb 24, 2020

Its not work. Multiple errors.
Severity Code Description Project File Line Suppression State
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 22
Error (active) E0898 nonmember operator requires a parameter with class or enum type Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0386 no instance of overloaded function "real" matches the required type Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 40
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 13
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 31
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 38
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 46
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 54
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 62
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0276 name followed by '::' must be a class or namespace name Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0020 identifier "setReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 15
Error (active) E0020 identifier "setReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 24
Error (active) E0020 identifier "setImaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 16
Error (active) E0020 identifier "setImaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 25
Error (active) E0020 identifier "right" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0020 identifier "imaginary" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 48
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0020 identifier "getReal" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 107
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 111
Error (active) E0020 identifier "Complex" is undefined Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 120
Error (active) E0065 expected a ';' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 121
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 72
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 84
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 98
Error (active) E0018 expected a ')' Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 111
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 38
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 46
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 70
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 81
Error (active) E1670 a type qualifier is not allowed on a nonmember function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 94
Error (active) E0258 'this' may only be used inside a nonstatic member function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 56
Error (active) E0258 'this' may only be used inside a nonstatic member function Complex c:\Users\Dima\Documents\Visual Studio 2017\Projects\ClassesAndMore\Complex\Complex.cpp 64

Try replace COMPLEX_H with anything else. It solved for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment