C++ Operator Overloading help

This is a discussion on C++ Operator Overloading help within the C++ Programming forums, part of the General Programming Boards category; I am having problems with my operators that I have overloaded. My current program is probably too long to post ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    C++ Operator Overloading help

    I am having problems with my operators that I have overloaded. My current program is probably too long to post here and if i did post it all i doubt anyone would look at it. So I'll just post the errors and some sections of code that might be useful. Thank you in advance for the help. The errors I get are:
    "card.h", line 27.14: 1540-1184 (S) Wrong number of parameters for "operator>".
    a4main.cpp:
    "card.h", line 27.14: 1540-1184 (S) Wrong number of parameters for "operator>".
    "a4main.cpp", line 457.8: 1540-0218 (S) The call does not match any parameter list for "operator!".
    "a4main.cpp", line 457.8: 1540-1283 (I) "builtin operator!(bool)" is not a viable candidate.

    Here are my operators
    For the CARD CLASS:
    Code:
    bool operator!(){
    		bool test = false;
    		if (this.value == JOKER)
    			test = true;
    		return test;
    	}
    	bool operator > (const Card& left, const Card& right){
    		bool test = false;
    		if (left.value == right.value){
    			if (left.suit > right.suit)
    				test = true;
    		}
    		else if (left.value > right.value)
    			test = true
    		return test;
    	}
    
    	bool operator < (const Card& left, const Card& right){
    		bool test = false;
    		if (right.value == left.value){
    			if (right.suit > left.suit)
    				test = true;
    		}
    		else if (right.value > left.value)
    			test = true;
    		return test;
    	}
    For the PLAY CLASS:
    Code:
    	bool operator!(){
    		bool test = false;
    		if (this.play = 0)
    			test = true;
    		return test;
    	}
    	bool operator > (const Play& left, const Play& right){
    		bool test = false;
    		if (left.play == 0 && right.play == 0){
    			if (left.numofcards > right.numofcards)
    				test = true;
    		}
    		if (left.play != 0 && right.play == 0)
    			test = true;
    		if (left.play != 0 && right.play != 0){
    			if (left.numofcards > right.numofcards)
    				test = true;
    			if (left.numofcards == 1 && right.numofcards == 1){
    				if(left.card[0].Face() > right.card[0].Face())
    					test = true;
    				else if (left.card[0].Face() = right.card[0].Face()){
    					if (left.card[0].Suit() > right.card[0].Suit())
    						test = true;
    				}
    			}
    			if (left.numofcards == 2 && right.numofcards == 2){
    				if (left.card[1].Face() > right.card[1].Face())
    					test = true;
    				else if (left.card[1].Face() == right.card[1].Face()){
    					if (left.card[1].Suit() > right.card[1].Suit())
    						test = true;
    				}
    			}
    			if (left.numofcards == 3 && right.numofcards == 3){
    				if (left.card[2].Face() > right.card[2].Face())
    					test = true;
    				else if (left.card[2].Face() == right.card[2].Face()){
    					if (left.card[2].Suit() > right.card[2].Suit())
    						test = true;
    				}
    			}
    			if (left.numofcards == 4 && right.numofcards == 4){
    				if (left.card[3].Face() > right.card[3].Face())
    					test = true;
    				else if (left.card[3].Face() == right.card[3].Face()){
    					if (left.card[3].Suit() > right.card[3].Suit())
    						test = true;
    				}
    			}
    			if (left.numofcards == 5 && right.numofcards == 5){
    				if (left.play == 5){
    					if (right.play < 5)
    						test = true;
    				}
    				if (left.play == 6){
    					if (right.play < 6)
    						test = true;
    				}
    				if (left.play == 7){
    					if (right.play < 7)
    						test = true;
    				}
    				if (left.play == 8){
    					if (right.play < 8 || right.play == 9)
    						test = true;
    				}
    				if (left.play == 9){
    					if (right.play < 8)
    						test = true;
    				}
    			}
    		}
    	}
    
    	bool operator < (const Play& left, const Play& right){
    		bool test = false;
    		if (right.play == 0 && left.play == 0){
    			if (right.numofcards > left.numofcards)
    				test = true;
    		}
    		if (right.play != 0 && left.play == 0)
    			test = true;
    		if (right.play != 0 && left.play != 0){
    			if (right.numofcards > left.numofcards)
    				test = true;
    			if (right.numofcards == 1 && left.numofcards == 1){
    				if(right.card[0].Face() > left.card[0].Face())
    					test = true;
    				else if (right.card[0].Face() = left.card[0].Face()){
    					if (right.card[0].Suit() > left.card[0].Suit())
    						test = true;
    				}
    			}
    			if (right.numofcards == 2 && left.numofcards == 2){
    				if (right.card[1].Face() > left.card[1].Face())
    					test = true;
    				else if (right.card[1].Face() == left.card[1].Face()){
    					if (right.card[1].Suit() > left.card[1].Suit())
    						test = true;
    				}
    			}
    			if (right.numofcards == 3 && left.numofcards == 3){
    				if (right.card[2].Face() > left.card[2].Face())
    					test = true;
    				else if (right.card[2].Face() == left.card[2].Face()){
    					if (right.card[2].Suit() > left.card[2].Suit())
    						test = true;
    					}
    				}
    				if (right.numofcards == 4 && left.numofcards == 4){
    					if (right.card[3].Face() > left.card[3].Face())
    						test = true;
    					else if (right.card[3].Face() == left.card[3].Face()){
    						if (right.card[3].Suit() > left.card[3].Suit())
    							test = true;
    					}
    				}
    				if (right.numofcards == 5 && left.numofcards == 5){
    					if (right.play == 5){
    						if (left.play < 5)
    							test = true;
    					}
    					if (right.play == 6){
    						if (left.play < 6)
    							test = true;
    					}
    					if (right.play == 7){
    						if (left.play < 7)
    							test = true;
    					}
    					if (right.play == 8){
    						if (left.play < 8 || left.play == 9)
    							test = true;
    					}
    					if (right.play == 9){
    						if (left.play < 8)
    							test = true;
    					}
    				}
    		}
    	}

    And this is where the operators are being tested in the testmain
    Code:
    /*Test Card's > operator.  Passed array that holds every card in a deck,
      an array that holds which tests failed and a reference to what test
      number we are on.  returns true if all tests passed
      false otherwise*/
    bool TestCardGreater(Card deck[],int failedtests[],int& testnumber){
      bool passmain=true;
      int k=0;
      cout << "Testing Card's > operator" << endl;
      for(int i=0;i<52;i+=4){
        for(int j=2;j<52;j+=4){
          if(i!=j){
    	if(( !GREATERTHAN1[k]&& (deck[i] > deck[j])) ||
    	  (GREATERTHAN1[k] && !(deck[i] > deck[j]))){
    	  failedtests[testnumber]=1;
    	  passmain=false;
    #if VERBOSE==1
    	  cout << "Card's > operator test failed." << endl;
    	  cout << "Your program reported that " << deck[i].Face() << deck[i].Suit();
    	  cout << " > " << deck[j].Face() << deck[j].Suit() << endl << endl;
    #endif
    	}
    	k++;
    	testnumber++;
          }
        }
      }
      return passmain;
    }
    /*Test Card's < operator.  Passed array that holds every card in a deck,
      an array that holds which tests failed and a reference to what test
      number we are on.  returns true if all tests passed
      false otherwise*/
    bool TestCardLesser(Card deck[],int failedtests[],int& testnumber){
      cout << "Testing Card's < operator" << endl;
      bool passmain=true;
      int k=0;
      for(int i=0;i<52;i+=4){
        for(int j=2;j<52;j+=4){
          if(i!=j){
    	if(( LESSTHAN1[k]&& !(deck[i] < deck[j])) ||
    	    (!LESSTHAN1[k] && (deck[i] < deck[j]))){
    	  failedtests[testnumber]=1;
    	  passmain=false;
    #if VERBOSE==1
    	  cout << "Card's < operator test failed." << endl;
    	  cout << "Your program reported that " << deck[i].Face() << deck[i].Suit();
    	  cout << " < " << deck[j].Face() << deck[j].Suit() << endl << endl;
    #endif
    	}
    	testnumber++;
    	k++;
          }
        }
      }
      return passmain;
    }
    /*Tests the < operator using Plays with different/invalid number of cards*/
    bool TestPlayLesser1(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int total=RSIZES[4];                   //total number of Play objects
      cout << "Testing Play's < Operator on Plays with different number of cards" << endl;
      for(int i=0;i<total;i++){
        for(int j=0;j<total;j++){
          if(i!=j && NUMCARDS[i]!=NUMCARDS[j]){
    	if(((NUMCARDS[i]<NUMCARDS[j]) && !(parray[i]<parray[j]))||
    	    (!(NUMCARDS[i]<NUMCARDS[j]) && parray[i]<parray[j])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[j].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[j]);
    	  cout << endl;
    #endif
    	}
    	testnumber++;
          }
        }
      }
      return passmain;
    }
    #endif
    
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with Singles*/
    bool TestPlayLesser2(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int k=0;
      cout << "Testing Play's < Operator on single cards" << endl;
      for(int i=0;i<52;i+=4){
        for(int j=2;j<52;j+=4){
          if(i!=j){
    	  if((!LESSTHAN1[k] && (parray[i] < parray[j])) ||
    	     (LESSTHAN1[k] && (!(parray[i] < parray[j])))){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[j].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[j]);
    	  cout << endl;
    	  cout << ((parray[i] < parray[j])?"true":"false")<< endl;
    #endif
    	}
    	k++;
    	testnumber++;
          }
        }
      }
    
      return passmain;
    }
    #endif
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with Singles*/
    bool TestPlayLesser3(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's < Operator on Pairs" << endl;
      for(int i=RSIZES[0];i<RSIZES[1];i++){
        for(int k=RSIZES[0];k<RSIZES[1];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((LESSTHAN2[j] && !(parray[i] < parray[k])) ||
    	    (!LESSTHAN2[j] && parray[i] < parray[k])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    #endif
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with Singles*/
    bool TestPlayLesser4(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's < Operator on 3 of a kinds" << endl;
      for(int i=RSIZES[1];i<RSIZES[2];i++){
        for(int k=RSIZES[1];k<RSIZES[2];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((LESSTHAN3[j] && !(parray[i] < parray[k])) ||
    	    (!LESSTHAN3[j] &&(parray[i] < parray[k]))){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,parray[i].NumCards());
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,parray[k].NumCards());
    	  cout << "Your < operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN3[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    #endif
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with 4 of a kinds*/
    bool TestPlayLesser5(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's < Operator on 4 of a kinds" << endl;
      for(int i=RSIZES[2];i<RSIZES[3];i++){
        for(int k=RSIZES[2];k<RSIZES[3];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((LESSTHAN4[j] && !(parray[i] < parray[k])) ||
    	    (!LESSTHAN4[j] &&(parray[i] < parray[k]))){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << "Your < operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN4[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    #endif
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with 4 of a kinds*/
    bool TestPlayLesser6(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's < Operator on Plays with 5 Cards" << endl;
      for(int i=RSIZES[3];i<RSIZES[4];i++){
        for(int k=RSIZES[3];k<RSIZES[4];k++){
          if(i!=k  &&!HasSame(PICK5[i-RSIZES[3]],PICK5[k-RSIZES[3]])
    	&& !(!parray[i]) && !(!parray[k])){
    	if((LESSTHAN5[j] && !(parray[i] < parray[k])) ||
    	    (!LESSTHAN5[j] &&(parray[i] < parray[k]))){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's < operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 < P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << "Your < operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN5[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    #endif
    
    
    #if TESTPLAY == ON
    
    /*Tests the > operator using Plays with different/invalid number of cards*/
    bool TestPlayGreater1(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int total=RSIZES[4];                   //total number of Play objects
      cout << "Testing Play's > Operator on Plays with different number of cards" << endl;
      for(int i=0;i<total;i++){
        for(int j=0;j<total;j++){
          if(i!=j && NUMCARDS[i]!=NUMCARDS[j]){
    	if(((NUMCARDS[i]>NUMCARDS[j]) && !(parray[i]>parray[j]))||
    	    (!(NUMCARDS[i]>NUMCARDS[j]) && parray[i]>parray[j])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[j].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[j]);
    	  cout << endl;
    #endif
    	}
    	testnumber++;
          }
        }
      }
      return passmain;
    }
    
    #endif
    
    
    #if TESTPLAY == ON
    
    /*Tests the < operator using Plays with Singles*/
    bool TestPlayGreater2(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int k=0;
      cout << "Testing Play's > Operator on single cards" << endl;
      for(int i=0;i<52;i+=4){
        for(int j=2;j<52;j+=4){
          if(i!=j){
    	  if((!GREATERTHAN1[k] && (parray[i] > parray[j])) ||
    	     (GREATERTHAN1[k] && (!(parray[i] > parray[j])))){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[j].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[j]);
    	  cout << endl;
    
    #endif
    	}
    	k++;
    	testnumber++;
          }
        }
      }
    
      return passmain;
    }
    #endif
    
    
    #if TESTPLAY == ON
    /*Tests the > operator using Plays with Singles*/
    bool TestPlayGreater3(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's > Operator on Pairs" << endl;
      for(int i=RSIZES[0];i<RSIZES[1];i++){
        for(int k=RSIZES[0];k<RSIZES[1];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((GREATERTHAN2[j] && !(parray[i] > parray[k])) ||
    	    (!GREATERTHAN2[j] && parray[i] > parray[k])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    
    #endif
    
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with Singles*/
    bool TestPlayGreater4(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's > Operator on 3 of a kinds" << endl;
      for(int i=RSIZES[1];i<RSIZES[2];i++){
        for(int k=RSIZES[1];k<RSIZES[2];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((GREATERTHAN3[j] && !(parray[i] > parray[k])) ||
    	    (!GREATERTHAN3[j] && parray[i] > parray[k])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,parray[i].NumCards());
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,parray[k].NumCards());
    	  cout << "Your > operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN3[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    
    #endif
    
    
    #if TESTPLAY == ON
    /*Tests the < operator using Plays with 4 of a kinds*/
    bool TestPlayGreater5(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's > Operator on 4 of a kinds" << endl;
      for(int i=RSIZES[2];i<RSIZES[3];i++){
        for(int k=RSIZES[2];k<RSIZES[3];k++){
          if(i!=k  && !(!parray[i]) && !(!parray[k])){
    	if((GREATERTHAN4[j] && !(parray[i] > parray[k])) ||
    	    (!GREATERTHAN4[j] && parray[i] > parray[k])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << "Your < operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN4[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    
    #endif
    
    
    #if TESTPLAY == ON
    /*Tests the > operator using Plays with 4 of a kinds*/
    bool TestPlayGreater6(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int j=0;
      cout << "Testing Play's > Operator on Plays with 5 Cards" << endl;
      for(int i=RSIZES[3];i<RSIZES[4];i++){
        for(int k=RSIZES[3];k<RSIZES[4];k++){
          if(i!=k  &&!HasSame(PICK5[i-RSIZES[3]],PICK5[k-RSIZES[3]])
    	&& !(!parray[i]) && !(!parray[k])){
    	if((GREATERTHAN5[j] && !(parray[i] > parray[k])) ||
    	    (!GREATERTHAN5[j] && parray[i] > parray[k])){
    	  passmain=false;
    	  failedtests[testnumber]=1;
    #if VERBOSE==ON
    	  Card getarr1[5],getarr2[5];
    	  parray[i].Get(getarr1);
    	  parray[k].Get(getarr2);
    	  cout << "The Play's > operator is not working properly" << endl;
    	  cout << "Your operator returned the wrong value when" << endl;
    	  cout << "Comparing P1 > P2 " << endl;
    	  cout << "P1 is ";
    	  PrintCardArray(getarr1,NUMCARDS[i]);
    	  cout << "P2 is ";
    	  PrintCardArray(getarr2,NUMCARDS[k]);
    	  cout << "Your < operator returns " <<((parray[i] < parray[k])?"true":"false") << endl;
    	  cout << "It should return " << (LESSTHAN5[j]?"true":"false") << endl;
    	  cout << endl;
    #endif
    	}
    	testnumber++;
    	j++;
          }
        }
      }
      return passmain;
    }
    #endif
    The ! operator is checked here, this part of the code is line 457 if(!(*c1)){
    Code:
    bool TestCardConstructor(int failedtests[],int& testnumber){
      Card* c1;
      int suitidx,faceidx;                //used to hold index of suit and face
      bool failedmain=false;
      cout << "Testing Card(char,int) constructor" << endl;
      for(int i=0;i<52;i++){
        suitidx=i%4;
        faceidx=i/4;
        char currsuit=(i%3)?SUITS[suitidx]:tolower(SUITS[suitidx]);  //uses the lower
    								 //case char once
    								 //in a while to
    								 //make sure it works
        c1=new Card(currsuit,FACES[faceidx]);                        //create a Card
    
        if(!(*c1)){                                                  //check ! operator
          failedtests[testnumber]=1;
          failedmain=true;
    Here is the PLAY ! operator
    Code:
    bool TestNotOperator(Play parray[], int failedtests[],int& testnumber){
      bool passmain=true;
      int total=RSIZES[4];                 //total number of Play objects
      cout << "Testing Play's ! operator" << endl;
      for(int i=0;i<total;i++){
        bool notrc=!parray[i];
        if((VALIDPLAYS[i] && notrc) || (!VALIDPLAYS[i]&& (!notrc))){
          passmain=false;
          failedtests[testnumber]=1;
    #if VERBOSE==ON
          cout << "The Play's !operator is not working properly" << endl;
          cout << "The operator should have returned "<< ((!VALIDPLAYS[i])?"true":"false")<< endl;
          cout << "Your function returned " << ((!parray[i])?"true":"false")<<endl;
          cout << i << endl;
          Card arr[5];
          int getrc=parray[i].Get(arr);
          PrintCardArray(arr,getrc);
    
          cout << endl;
    #endif
        }
        testnumber++;
      }
      return passmain;
    }
    #endif

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Instead of making any assumptions about your design or what your particular problem is, review the example below and insure you try something similar. Also consider the following couple of notes: 1) different operators require a different number of parameters; 2) if you define an operator inside a class/struct, the first parameter is implied (the object itself); 3) the exception to #2 is if you've actually defined the operator as a friend (because in such a case the function is actually not a member).
    Code:
    class Operators {
      int value;
    public:
      // negation operator 
      bool operator!() const;
      
      // addition operator
      int operator+(const Operators &rhs) const;
    
      // insertion operator
      friend ostream& operator<<(ostream &out, const Operators &obj);
    };
    
    // this is a unary operator meaning it has a single argument and
    // in this case it is implied; it is used like this: !obj
    bool Operators::operator!() const {
      return value == 0;
    }
    
    // this is a binary operator; it is used like this: obj1 + obj2
    int Operators::operator+(const Operators &rhs) const {
      return value + rhs.value;
    }
    
    // this is a binary operator but the first argument is explicitly
    // defined because it is not a member function; it
    // is used like this: cout << obj << endl;
    ostream& operator<<(ostream &out, const Operators &obj) {
      return out << obj.value;
    }

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Thanks I forgot about rule 2...oops.

Popular pages Recent additions subscribe to a feed

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