Перегрузка <<
От: enots  
Дата: 19.09.03 08:18
Оценка:
Вобщем решил потренироваться с перегрузкой




// modulus.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <stdio.h> 
#include <functional> 
#include <iostream> 

using namespace std ; 

class MathOps : public plus<int>, public minus<int>, 
                 public multiplies<int>, public divides<int>, 
                 public modulus<int> 
{ 
public: 
  int value; 
  MathOps(){value=0;} 
  MathOps(int x){value=x;} 
  result_type operator+(second_argument_type add2) 
                            {return value + add2;} 
  result_type operator-(second_argument_type sub2) 
                            {return value - sub2;} 
  result_type operator*(second_argument_type mult2) 
                            {return value * mult2;} 
  result_type operator/(second_argument_type div2) 
                            {return value / div2;} 
  result_type operator%(second_argument_type mod2) 
                            {return value % mod2;} 
  ostream& operator<<( ostream& os ) 
  { 
      os << this->value ; 
       return os ; 
  } 

}; 
/* 
ostream& operator<<(ostream& os, const MathOps& obj ) 
{ 
       os << obj.value ; 
       return os ; 
} 
*/ 
void main(void) 
{ 
  MathOps one,two,three,four,five,six; 

  cout << "Using MathOps class..." << endl ; 

  one = 18; 
  cout << "one = " << one << endl ; 
/* 
  two = one + 1; 
  cout << "two = one + 1 = " << two << endl ; 

  three = two - 2; 
  cout << "three = two - 2 = " << three << endl ; 

  four = three * 3; 
  cout << "four = three * 3 = " << four << endl ; 

  five = four / 4; 
  cout << "five = four / 4 = " << five << endl ; 

  six = five % 5; 
  cout << "six = five % 5 = " << six << endl ; 
  */ 
}


а компилятор выдаёт:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class MathOps' (or there is no acceptable conversion)

не пойму в чём дело
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.