0

Error:

expected ‘,’ or ‘...’ before ‘&’ token
randomNumberMagnifier.h:32: error: ISO C++ forbids declaration of ‘randomNumberMagnfier’ with no type
randomNumberMagnifier.h:32: error: ‘std::ostream& randomNumberMagnifier::operator<<(std::ostream&, int)’ must take exactly one argument
[singha1@cs1 p4]$ 

This is the error that keeps showing up. I have pretty much copied the code for the overloaded operator<< to print the values user these operators.

.h:

#ifndef RANDOMNUMBERMAGNIFIER_H
#define RANDOMNUMBERMAGNIFIER_H
#include <iostream>
#include <fstream>

using namespace std;
class randomNumberMagnifier
{
  int addFactor;
  int multFactor;
  bool addOn;
  bool multOn;
  int randomNumber;
  static const int MAX_ADD_FACTOR = 100;
  static const int MAX_MULT_FACTOR = 20;
  static const int MAX_RANDOM = 200;

 public:
  randomNumberMagnifier();

  randomNumberMagnifier(const randomNumberMagnifier& arandom);
  //~randomNumberMagnifer();                                                         

  randomNumberMagnifier& operator=(const randomNumberMagnifier& rhs );
  //  randomNumberMagnifier& operator==(const randomNumberMagnfier& rhs );           
  ostream&  operator<<(ostream& o, const randomNumberMagnfier& rhs );
  randomNumberMagnifier(bool aState, bool mState);

  int randomMagnifier();

  int getAdd();
  int getMult();

  bool getAddState();
  bool getMultState();

  int getRandomNumber();

  void display(ostream& out) const;



};
#endif

.cpp:

ostream& operator<<(operator& out, const randomNumberMagnifier& rhs)
{
  rhs.display(out);
  return out;
  //cout << this->getAdd()=rhs.getAdd();                                                  
  /*                                                                                      
  cout << this->multFactor;                                                               
  cout << this->addOn;                                                                    
  cout << this->multOn;                                                                   
  cout << this->randomNumber;                                                             
  */
}

void randomNumberMagnifier :: display(ostream& out)const
{
  out << addFactor << multFactor << randomNumber;
}
jogojapan
  • 68,383
  • 11
  • 101
  • 131
AAA
  • 35
  • 2
  • 12
  • It's just a typo in your code. You wrote `randomNumberMagnfier` instead of `randomNumberMagnifier` – Gorpik Nov 05 '12 at 07:58
  • There is still this error error: ‘std::ostream& randomNumberMagnifier::operator<<(std::ostream&, const randomNumberMagnifier&)’ must take exactly one argument – AAA Nov 05 '12 at 08:04

1 Answers1

1
  ostream&  operator<<(ostream& o, const randomNumberMagnfier& rhs );

randomNumberMagnfier is misspelled.

EDIT

To clarify the second error you are getting:

error: ‘std::ostream& randomNumberMagnifier::operator<<(std::ostream&, const randomNumberMagnifier&)’ must take exactly one argument

You need to implement this as a nonmember function.

As best I can tell, the reason you cannot implement it as a member of the class comes from 13.5.2.1 in the standard:

13.5.2 Binary operators

1 A binary operator shall be implemented either by a non-static member function (9.3) with one parameter or by a nonmember function with two parameters. Thus, for any binary operator @, x@y can be interpreted as either x.operator@(y) or operator@(x,y). If both forms of the operator function have been declared, the rules in 13.3.1.2 determine which, if any, interpretation is used.

That is, if you're overloading a binary operator and it's a member function then it must take one parameter. You need two parameters, therefore you must use a nonmember function.

Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • thank you!!! It took me a while even after you guys pointed it out. – AAA Nov 05 '12 at 08:02
  • there is still this error error: ‘std::ostream& randomNumberMagnifier::operator<<(std::ostream&, const randomNumberMagnifier&)’ must take exactly one argument – AAA Nov 05 '12 at 08:04
  • @AmaninderSingh http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream suggests implementing this function outside the class. – ta.speot.is Nov 05 '12 at 08:10
  • Indeed, `operator<<` should never be a member function; otherwise, the operands will be in reverse order. – Gorpik Nov 05 '12 at 08:21
  • it worked but Could you exlain why it need to implement out the class? – AAA Nov 05 '12 at 08:25
  • @AmaninderSingh You could implement is as a class member of `std::basic_ostream` (as it's the first parameter) but then you'd need to recompile your standard library I guess. – moooeeeep Nov 05 '12 at 08:45