Class Compilation Problem

This is a discussion on Class Compilation Problem within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm trying to compile some example source code from my MCSE coursework, which is supposed to demonstrate the ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Unhappy Class Compilation Problem

    Hey all,

    I'm trying to compile some example source code from my MCSE coursework, which is supposed to demonstrate the use of operator overloading. Problem is, VC++ 6.0 SP5 dies. Here's the error I get:

    D:\MCSE\Book2\Chapter11\5.cpp(48) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1794)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
    Error executing cl.exe.

    Now here's the code:

    Code:
    #include <iostream.h>
    
    class Coordinates
    {
    	protected:
    		int xcoord, ycoord;
    	public:
    		Coordinates(void);
    		Coordinates(int x1, int y1);
    		Coordinates operator + (Coordinates inCoord);
    		Coordinates operator - (Coordinates *inCoord);
    		void show(void);
    };
    
    int main(void)
    {
    	Coordinates Coord1(10, 10), Coord2(2, 2);
    	Coordinates Coord3, Coord4;
    
    	cout << "Here are the current values of Coord 1 and 2:\n";
    	Coord1.show();
    	Coord2.show();
    
    	Coord3 = Coord1 + Coord2;
    	Coord4 = Coord1 - &Coord2;
    
    	cout << "Coord1 + Coord2:\n";
    	Coord3.show();
    	cout << "Coord1 - Coord2:\n";
    	Coord4.show();
    
    	return 0;
    }
    
    Coordinates::Coordinates(void)
    {
    	xcoord = 0;
    	ycoord = 0;
    }
    
    Coordinates::Coordinates(int x1, int y1)
    {
    	xcoord = x1;
    	ycoord = y1;
    }
    
    Coordinates::Coordinates::operator + (Coordinates inCoord)
    {
    	Coordinates temp_coord;
    	temp_coord.xcoord = xcoord + inCoord.xcoord;
    	temp_coord.ycoord = ycoord + inCoord.ycoord;
    
    	return temp_coord;
    }
    
    Coordinates::Coordinates::operator - (Coordinates *inCoord)
    {
    	Coordinates temp_coord;
    	temp_coord.xcoord = xcoord - inCoord->xcoord;
    	temp_coord.ycoord = ycoord - inCoord->ycoord;
    
    	return temp_coord;
    }
    
    void Coordinates::show(void)
    {
    	cout << "Value of X coordinate: " << xcoord << endl
    		 << "Value of Y coordinate: " << ycoord << endl;
    }
    After some digging around, I managed to find this Microsoft Knowledge Base Article outlining the problem. However, to be perfectly honest, classes are so new to me, that I don't really know exactly how to fix the problem.

    I've tried a few different things with the code now, all of which end up with the same error. Does anyone mind taking a quick look through? I'd be very grateful.

    Thanks in advance,

    John.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The problem is with your operator + and - overloading. You don't need the scope resolution operator between the return type and class name.

    I.E. - You had...
    Code:
    Coordinates::Coordinates::operator - (Coordinates *inCoord)
    {
      Coordinates temp_coord;
      temp_coord.xcoord = xcoord - inCoord->xcoord;
      temp_coord.ycoord = ycoord - inCoord->ycoord;
    
      return temp_coord;
    }
    The first line should be..
    Code:
    Coordinates Coordaintes::operator -(.....
    Likewise for the opeartor +


    Good Luck.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Oh dear, lol, I feel like such an idiot. Can't believe I didn't spot that. Not even class-related, it's a return-type lol. Ugh, been up all night studying. Thank you very much for pointing that out. One of those days I guess.

    --John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  2. Class problem
    By w274v in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 05:53 PM
  3. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 11:47 AM
  4. class, set, < problem
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2003, 09:25 AM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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