Operator overloading MSVC++

This is a discussion on Operator overloading MSVC++ within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble getting the following operator to overload. I want to take two inputs of type BMap24b and get ...

  1. #1
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12

    Question Operator overloading MSVC++

    I'm having trouble getting the following operator to overload. I want to take two inputs of type BMap24b and get either an int or a bool, depending on the recieving tupe. Any help would be appreciated.

    Code:
    public:
    	int operator &( BMap24b a, BMap24b b );
    	bool operator &( BMap24b a, BMap24b b );
    
    
    
    
    int BMap24b:: operator&(BMap24b a, BMap24b b)
    {
    	int counter = 0;
    
    	if( a.Color[0].i == b.Color[0].i )
    	{
    		counter += 0;
    	}
    	else {
    		if( a.Color[0].i > b.Color[0].i )
    		{
    			counter -= 1;
    		}
    		else {
    			counter += 1;
    		}
    	}
    
    	if( a.Color[1].i == b.Color[1].i )
    	{
    		counter += 0;
    	}
    	else {
    		if( a.Color[1].i > b.Color[1].i )
    		{
    			counter -= 1;
    		}
    		else {
    			counter += 1;
    		}
    	}
    
    	if( a.Color[2].i == b.Color[2].i )
    	{
    		counter += 0;
    	}
    	else {
    		if( a.Color[2].i > b.Color[2].i )
    		{
    			counter -= 1;
    		}
    		else {
    			counter += 1;
    		}
    	}
    
    	return( counter );
    }
    
    bool BMap24b:: operator&( BMap24b a, BMap24b b )
    {
    	bool flag;
    	int i;
    
    	i = a & b;
    	if( i == 0 )
    	{
    		flag = 1;
    	} else {
    		flag = 0;
    	}
    
    	return( flag );
    }

    In the same program I have two Global CStrings, SrcFile and TrgtFile, I can't figure out how to write to them from a class that has the file with them included. Once again, any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int operator &( BMap24b a, BMap24b b );
    bool operator &( BMap24b a, BMap24b b );

    There is no way for the compiler to differentiate between the parameters of the two prototypes listed above. Therefore you have to do it somehow in your code. Then when you have determined how to tell when to return an int and when to return a bool you can make that happen, in a sense anyway.

    If you want to be able to return one of two different types I would use a struct as a return type. The struct to have both possibities as data members.

    Code:
    struct Answer
    {
      int  counter;
      bool flag;
    };
    
    class whatever
    {
       //other stuff
       Answer operator &( BMap24b a, BMap24b b );
    };
    
    Answer operator &( BMap24b a, BMap24b b )
    {
       Answer answer;
       //determine whether you are going to initialize counter or flag
       //do the manipulation
       //change the value of counter or flag
       return answer;
    }
    I suppose you could try using a union insetead of a struct, but I haven't used that construct at all in code that I've written.

    Also: I have a problem with the following code:
    Code:
    bool BMap24b:: operator&( BMap24b a, BMap24b b )
    {
    	bool flag;
    	int i;
    
    	i = a & b;
    you are using the & operator with types for which you are trying to define the & operator. That seems illogical to me.

  3. #3
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12
    The use of & that you mentioned was an attempt at saving code. I'll try that union idea, it should work. I'm actually using unions in the BMap24b class.

    Still looking for an explanation of the global CString problem.

    New code :
    Code:
    public:
    	Bool_Int operator&( BMap24b a, BMap24b b );
    I'm getting a error saying:

    Code:
    error C2804: binary 'operator &' has too many parameters
    Last edited by AeroHammer; 04-14-2003 at 09:03 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm getting a error saying:

    error C2804: binary 'operator &' has too many parameters

    Well as far as error messages go, that's as clear as it gets. You say its a global function, yet the code shows it to be a class function. It's a "binary" operator so there can only be two arguments. You have the object on the left hand side for which the operator is called on and then two parameters on the right:

    object_lhs & (object_a) object_b???

    That's 3 objects.

  5. #5
    Registered User AeroHammer's Avatar
    Join Date
    Sep 2002
    Posts
    12
    Thanks, and the CStrings are Globals, not the function. Must not have made that clear enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with MSVC, Mingw32 and Code::Blocks
    By Brafil in forum C Programming
    Replies: 11
    Last Post: 10-12-2009, 12:34 PM
  2. MSVC resource script in Dev-C++
    By josh_d in forum Windows Programming
    Replies: 0
    Last Post: 03-17-2004, 04:07 PM
  3. MSVC behaves strange
    By ripper079 in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2003, 08:34 PM
  4. Blender3D, XML, and MSVC ???
    By Grumpy_Old_Man in forum Game Programming
    Replies: 0
    Last Post: 08-24-2003, 08:00 PM
  5. GCC (cygwin) much faster than MSVC, Borland?
    By Sargnagel in forum C Programming
    Replies: 15
    Last Post: 08-05-2003, 04:15 AM

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