Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a program in TC compiler..and getting an error but could not understand
C++
#include<iostream.h>
#include<conio.h>
class aman
{
	int real;
	int imag;
	public : aman(int a,int b)
		{
			real=a;
			imag=b;
		}
	       void showdata()
		{
			cout<<real<<"+i"<<imag<<endl;
		}
		 aman operator+(aman);
};
       aman aman::operator + (aman c)
	      {

		aman temp;

		temp.real=real+ c.real;
		temp.imag=imag+ c.imag;
		return (temp);
	      }
void main()
{
	clrscr();
	aman d,e,f;
       d=aman (5,6);
       e= aman (6,9);
	f=d+e;
	d.showdata();
	cout<<"a is";
	e.showdata();
	cout<<"b is";
	f.showdata();

	cout<<"f=d+e is:"<<endl;
	f.showdata();
	getch();
}


Error: could not find match for ‘aman::aman()’

and it is pointing on
C++
aman aman::operator + (aman c)
	      {

		aman temp;
		temp.real=real+ c.real;
		temp.imag=imag+ c.imag;
		return (temp);
	      }

Thanks in advance
Posted
Updated 3-Nov-11 1:24am
v2

Once you provide a parameterized constructor, the compiler doesnt provide you the default plain one. You need to define your plain ctor manually.

aman::aman()
{
}
 
Share this answer
 
Comments
shivani 2013 3-Nov-11 8:27am    
Thank you,the program has been run but I am not getting one thing that why compiler doesn't provide default one.........as i have read that default constructors are automatically called.
ShilpiP 3-Nov-11 8:47am    
Compiler automatically created default constructor if you are not creating any constructor else not.
You should also be passing by reference or pointer into the + operator function.
Pass by value is the default, and is what is happening here. This creates a full copy of the variable, then calls the function with this copy, then deletes the copy. Pass by reference and pass by pointer just pass a copy of the memory address. This is quicker, especially for classes which hold lots of data

C++
class aman {
	//other functions here
	aman operator +(const aman&);
};

aman aman::operator +(const aman &c) {
	aman temp;
	temp.real = real + c.real;
	temp.imag = imag + c.imag;
	return (temp);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900