operator overloading problem

This is a discussion on operator overloading problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> #include<conio.h> class location { public: int xposition; int yposition; location(){} location(int x, int y) { xposition=x; yposition=y; } ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    72

    operator overloading problem

    Code:
    #include<iostream.h>
    #include<conio.h>
    class location
    {
    		public:
    				 int xposition;
    				 int yposition;
    
    				 location(){}
    				 location(int x, int y)
    				 {
    								  xposition=x;
    								  yposition=y;
    								  }
    
    								  void show()
    								  {
    										 cout<<"Position x = "<<xposition;
    										 cout<<"Position y = "<<yposition;
    										 }
    										 location operator+(location obj1)
    										 {
    										 location temp;
    										 temp.xlocation=xlocation+obj2.x;
    										 temp.ylocation=ylocation+obj2.y;
    										 return temp;
    										 }
    
    										 };
    										 void main()
    										 {
    												clrscr();
    												location obj1(20,30);
    												location obj2(30,40);
    												location obj3;
    												obj1.show();
    												obj2.show();
    												obj3=obj1+obj2;
    												obj3.show();
    												getch();
    												}
    I want to overload the operator + so addition can be done ;\ all the other code is fine but something is wrong in the operator+ fucntion.. can someone help? tanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why is your indentation continually going to the right? That is not the purpose of indentation.

    Code:
    										 location operator+(location obj1)
    										 {
    										 location temp;
    										 temp.xlocation=xlocation+obj2.x;
    										 temp.ylocation=ylocation+obj2.y;
    										 return temp;
    										 }
    obj2 is not declared in this scope. Which the compiler is probably saying by "undeclared identifier obj2" and quite likely some other errors as a consequence.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    In addition to matsp's comments, the class has two members named xposition and yposition. But operator+() is working with some things named xlocation and ylocation.

    Either way, your compiler will be complaining about undeclared identifiers/variables/names (or some similar message: the precise wording depends on the compiler).

    There is tremendous practical value in actually reading error messages from a compiler before giving up and asking questions in a forum.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grumpy View Post
    There is tremendous practical value in actually reading error messages from a compiler before giving up and asking questions in a forum.
    Hear, Hear! I agree with that. Learning to read compilers error messages and connecting that with what you have just done wrong [it helps if you compile often in the beginning, as having hundreds or thousands of errors in a few hundred lines of new code can make it very difficult to figure out which error is caused directly by something you done wrong, and which errors are because the compiler derailed and couldn't find it's way back to valid code].

    The key to fixing errors, whether you have one or many is to fix them one at a time, and start with the one in the first line that gives an error - that way, you don't try to fix/understand errors that are caused by some "derailment" of the compiler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21