5 Replies - 621 Views - Last Post: 11 April 2015 - 11:43 AM Rate Topic: -----

#1 fettm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 15

Need help fixing errors in class declaration/definition

Posted 10 April 2015 - 02:41 PM

This code is meant to open a file and use overloaded operators for a complex number class. I am getting a lot of errors in my class declaration/definition but I am not sure why. Please take a look and see if you can spot anything!!


#include <iostream>
#include <cmath>
#include <fstream>

class CN
{
	public: 
		double real;
		double im;
	
		CN();
		CN(double, double);
		CN(double);
		double getReal();
		double getIm();
		void setReal(double);
		void setIm(double);
		bool operator ==(CN z, CN w);
		friend bool areequal(CN z);
		bool CN::operator == (CN z, CN w);
		bool CN::operator < (CN z, CN w);
		friend CN operator * (CN z, CN w);
		friend CN operator + (CN z, CN w);
		friend CN operator();
		friend ostream& operator << (ostream& outs, CN z);
		friend istream& operator >> (istream& ins, CN& z);
};

CN::CN()
{
	real= 0;
	im= 0;
}

CN::CN(double x, double y)
{
	real= x;
	im= y;
}

double CN::getReal()
{
	return real;
}

double CN::getIm()
{
	return im;
}
void CN::setReal(double x)
{
	real= x;
}

void CN::setIm(double y)
{
	im= y;
}

bool CN::areequal(CN z)
{
	return (real==z.real && z.im);
}

bool CN::operator == (CN z, CN w)
{
	return z.real==w.real && z.im==w.im;
}

bool CN::operator < (CN z, CN w)
{
     return sqrt((z.real*z.real) + (z.im*z.im)) > sqrt((w.real*w.real) + (w.im *w.im));
}

CN operator * (CN z, CN w)
{
       CN product;
       product.real = (z.real*w.real) - (z.im*w.im)
       product.im = (z.real*w.im)+ (w.real*z.im);
       
}

CN operator + (CN z, CN w)
{
       CN sum;
       sum.real = (z.real + w.real);
       sum.im = (z.im + w.im);
}

CN operator - ()
{
	CN temp;
	temp.real= -real;
	temp.im= -im;
	return temp;
}

ostream& operator <<(ostream& outs, CN& z)
{
    if (z.im < 0.0)
    {
       outs << z.real << z.im; 
    }
    else if (z.im == 1)
    {
       outs << z.real << " + i";
    else if (z.im == -1)
    {
       outs << z.real << " - i";
    }
    else if (z.real == 0.0)
    {
       outs << z.im << "i";
    }
    else if (z.im == 0.0)
    {
       outs << z.real;
    }
    else
	{
    outs << z.real << " + " << z.im << "i";
    }
	return outs;
}

istream& operator >> (istream& ins, CN& z)
{
	ins << z.real << z.im;
	return ins;
}



using namespace std;
int main()
{
    CN array[10];
	istream ins;
	ostream outs;
	ins.open("P:\Class\Math\Aydin\118\FilesForLabs\inputdata.txt");
	outs.open("outputdata.txt");
	
	for (int i=0; i<9; i++)
	    {
        ins >> array[i];
        cout << array[i];
        outs << array[i];
        }
     cout << "Please enter one more complex number.\n";
     cin >> array[9];
     outs << array[9];
     
     for (int k=0; k<10; k++)
     {
      cout << array[k];
     }
     
	
	sort(array);

return 0;

};
	
void sort (CN array[10])
{
	int index_of_next;
	for (int index=0; index < 10; index++)
	{
		index_of_next = index_of_smallest(array, index);
		swap_values(array, index, index_of_next);
	}
}
	
void swap_values(CN array[10], int i, int j)
{
	CN temp= array[i];
	temp = array[i];
	array[i] = array[j];
	array[j]= temp;
}


int index_of_smallest(CN array[10], int index)
{
	CN min = array[index];
	int index_of_min = index;
	
	for (int k = index + 1; k <10; k++)
	{
		if(array[k] < min)
		{
			min = array[k];
			index_of_min = k;
		}
	}
	return index_of_min;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Need help fixing errors in class declaration/definition

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13553
  • View blog
  • Posts: 54,085
  • Joined: 12-June 08

Re: Need help fixing errors in class declaration/definition

Posted 10 April 2015 - 02:49 PM

Please copy/paste your errors here.
Was This Post Helpful? 0
  • +
  • -

#3 fettm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 15

Re: Need help fixing errors in class declaration/definition

Posted 10 April 2015 - 02:59 PM

These are the error messages:

Lab_ComplexNumbers.cpp:46:8: error: overloaded 'operator==' must be a binary operator (has 3 parameters)
bool operator ==(CN z, CN w);
^
Lab_ComplexNumbers.cpp:48:12: error: extra qualification on member 'operator=='
bool CN::operator == (CN z, CN w);
~~~~^
Lab_ComplexNumbers.cpp:49:12: error: extra qualification on member 'operator<'
bool CN::operator < (CN z, CN w);
~~~~^
Lab_ComplexNumbers.cpp:49:12: error: overloaded 'operator<' must be a binary operator (has 3 parameters)
Lab_ComplexNumbers.cpp:52:13: error: friends can only be classes or functions
friend CN operator();
^
Lab_ComplexNumbers.cpp:53:10: error: unknown type name 'ostream'; did you mean 'std::ostream'?
friend ostream& operator << (ostream& outs, CN z);
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:53:32: error: unknown type name 'ostream'; did you mean 'std::ostream'?
friend ostream& operator << (ostream& outs, CN z);
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:54:10: error: unknown type name 'istream'; did you mean 'std::istream'?
friend istream& operator >> (istream& ins, CN& z);
^~~~~~~
std::istream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:146:38: note: 'std::istream' declared here
typedef basic_istream<char> istream;
^
Lab_ComplexNumbers.cpp:54:32: error: unknown type name 'istream'; did you mean 'std::istream'?
friend istream& operator >> (istream& ins, CN& z);
^~~~~~~
std::istream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:146:38: note: 'std::istream' declared here
typedef basic_istream<char> istream;
^
Lab_ComplexNumbers.cpp:88:10: error: out-of-line definition of 'areequal' does not match any declaration in 'CN'
bool CN::areequal(CN z)
^~~~~~~~
Lab_ComplexNumbers.cpp:106:52: error: expected ';' after expression
product.real = (z.real*w.real) - (z.im*w.im)
^
;
Lab_ComplexNumbers.cpp:118:4: error: overloaded 'operator-' must have at least one parameter of class or enumeration type
CN operator - ()
^
Lab_ComplexNumbers.cpp:121:14: error: use of undeclared identifier 'real'; did you mean 'CN::real'?
temp.real= -real;
^~~~
CN::real
Lab_ComplexNumbers.cpp:36:10: note: 'CN::real' declared here
double real;
^
Lab_ComplexNumbers.cpp:121:14: error: invalid use of non-static data member 'real'
temp.real= -real;
^~~~
Lab_ComplexNumbers.cpp:122:12: error: use of undeclared identifier 'im'; did you mean 'CN::im'?
temp.im= -im;
^~
CN::im
Lab_ComplexNumbers.cpp:37:10: note: 'CN::im' declared here
double im;
^
Lab_ComplexNumbers.cpp:122:12: error: invalid use of non-static data member 'im'
temp.im= -im;
^~
Lab_ComplexNumbers.cpp:126:1: error: unknown type name 'ostream'; did you mean 'std::ostream'?
ostream& operator <<(ostream& outs, CN& z)
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:126:22: error: unknown type name 'ostream'; did you mean 'std::ostream'?
ostream& operator <<(ostream& outs, CN& z)
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:135:5: error: expected expression
else if (z.im == -1)
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5343
  • View blog
  • Posts: 16,672
  • Joined: 25-December 09

Re: Need help fixing errors in class declaration/definition

Posted 10 April 2015 - 04:08 PM

Start with your first error, do you understand what it is trying to tell you?

Quote

Lab_ComplexNumbers.cpp:46:8: error: overloaded 'operator==' must be a binary operator (has 3 parameters)
bool operator ==(CN z, CN w);


This is telling you in cryptic speak that you have too many parameters. You can only have one parameter.

Fix this error then recompile and see how many of the other errors are fixed.

Also note your error messages don't match the code you provided. The above error should be on line 46, but it appears to be on about line 65.

Edit: You may find this link helpful.

Jim

This post has been edited by jimblumberg: 10 April 2015 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 fettm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 15

Re: Need help fixing errors in class declaration/definition

Posted 11 April 2015 - 10:56 AM

Would you mind telling me how I would only use one parameter if the definition only makes sense if two parameters are passed to it??

Thanks!!!
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: Need help fixing errors in class declaration/definition

Posted 11 April 2015 - 11:43 AM

The other implied parameter is the value stored within your instance of the the object.

In pseudo code, the equality operator is:
class Foo
{
    bool operator ==(Foo other)
    {
        return this->value == other.value;
    }

    int value;    // the type need not be value, 
                  // and it can be more complex than just a single field.
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1