c and c++ problem

c++.cpp no matching function for call to `Point::Point(Point)'

Page 1 of 1

4 Replies - 554 Views - Last Post: 21 January 2010 - 01:42 AM Rate Topic: -----

#1 bestsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

c and c++ problem

Posted 18 January 2010 - 09:00 PM

It will rise a probem which "c++.cpp no matching function for call to `Point::Point(Point)'" when i complie the code
with DEV-c++ .
	   class Point
  {
   public:
		  Point(int xx=0,int yy=0){X=xx,Y=yy;}
		  Point(Point&p);
		  int GetX()
		  { return X; }
		  int GetY()
		  { return Y; }
   private:
		   int X,Y;			
  };
Point::Point(Point&p)
		  {			   
			  X=p.X;			
			  Y=p.Y;		   
		  cout<<"Copy constructor called"<<endl; 
		  }
int main()
{  void fun1(Point p);
   Point fun2();
   Point A(4,5);
   Point B(A);   
   cout<<B.GetX()<<endl;
  fun1(B);
  B=fun2();				   // HERE there is a problem
  cout<<B.GetX()<<endl;
  getch();	  
	  }
void fun1(Point p)
{
 cout<<p.GetX()<<endl;	
}
Point fun2()
{
  Point A(1,2);
  return(A);   
}   


Is This A Good Question/Topic? 0
  • +

Replies To: c and c++ problem

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: c and c++ problem

Posted 18 January 2010 - 09:22 PM

I think you have not declared the object of class Point
you should declare the object B of class point and then try the program

tell me if it does not work
Was This Post Helpful? 0
  • +
  • -

#3 bestsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: c and c++ problem

Posted 18 January 2010 - 09:29 PM

View Postpdkharkar, on 18 Jan, 2010 - 08:22 PM, said:

I think you have not declared the object of class Point
you should declare the object B of class point and then try the program

tell me if it does not work

sorry it does not work.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: c and c++ problem

Posted 19 January 2010 - 12:11 AM

You had several problems... you need to watch it with Dev++ because its indention style is TERRIBLE and it can make you code convoluted and hard to read (and if it is hard to read it is hard to spot errors).

The actual error comes from your copy constructor, you need to throw a 'const' in there to ensure that the original object is not being edited:

#include <iostream>
#include <conio.h>
using namespace std;

class Point  {
   public:
		  Point(int xx=0,int yy=0){X=xx,Y=yy;}
		  Point(const Point& p);
		  int GetX() { return X; }
		  int GetY() { return Y; }
   private:
		   int X,Y;			
  };
Point::Point(const Point& p)
{			  
	X=p.X;			
	Y=p.Y;		  
	cout<<"Copy constructor called"<<endl;
}

void fun1(Point p);
Point fun2();

int main()
{
	Point A(4,5);
	Point B(A);  
	cout<<B.GetX()<<endl;
	fun1(B);
	B=fun2();				   // HERE there is a problem
	cout<<B.GetX()<<endl;
	getch();
	return 0;
}

void fun1(Point p)
{
	cout<<p.GetX()<<endl;	
}

Point fun2()
{
	Point A(1,2);
	return A;  
}   



Remember to keep your code clean and legible... it will help a great deal.
Was This Post Helpful? 1
  • +
  • -

#5 bestsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: c and c++ problem

Posted 21 January 2010 - 01:42 AM

View PostNickDMax, on 18 Jan, 2010 - 11:11 PM, said:

You had several problems... you need to watch it with Dev++ because its indention style is TERRIBLE and it can make you code convoluted and hard to read (and if it is hard to read it is hard to spot errors).

The actual error comes from your copy constructor, you need to throw a 'const' in there to ensure that the original object is not being edited:

#include <iostream>
#include <conio.h>
using namespace std;

class Point  {
   public:
		  Point(int xx=0,int yy=0){X=xx,Y=yy;}
		  Point(const Point& p);
		  int GetX() { return X; }
		  int GetY() { return Y; }
   private:
		   int X,Y;			
  };
Point::Point(const Point& p)
{			  
	X=p.X;			
	Y=p.Y;		  
	cout<<"Copy constructor called"<<endl;
}

void fun1(Point p);
Point fun2();

int main()
{
	Point A(4,5);
	Point B(A);  
	cout<<B.GetX()<<endl;
	fun1(B);
	B=fun2();				   // HERE there is a problem
	cout<<B.GetX()<<endl;
	getch();
	return 0;
}

void fun1(Point p)
{
	cout<<p.GetX()<<endl;	
}

Point fun2()
{
	Point A(1,2);
	return A;  
}   



Remember to keep your code clean and legible... it will help a great deal.

Thanks for your advices it does work!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1