views:

84

answers:

3

I have created an enumerated data type to define possible flight lengths. I'd like to overload its << operator so the representation is nicer.

When I compile this, I get the following error (posted for completeness' sake. Basically multiple definitions of operator <<(ostream&, Categoria&)):

g++ -oProjectoAEDA.exe src\voo.o src\tui.o src\tripulante.o src\tipoaviao.o src\manga.o src\main.o src\datahora.o src\companhiaaerea.o src\aviao.o src\aeroporto.o
src\tripulante.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\tipoaviao.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\manga.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\main.o: In function `ZlsRSoR9Categoria':
C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\companhiaaerea.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\aviao.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
src\aeroporto.o: In function `ZlsRSoR9Categoria':
c:/mingw/bin/../lib/gcc/i686-pc-mingw32/4.5.1/../../../../include/c++/4.5.1/new:103: multiple definition of `operator<<(std::ostream&, Categoria&)'
src\voo.o:C:\Users\Francisco\workspace_aeda\ProjectoAEDA\Debug/../src//headers/categoria.h:20: first defined here
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 928  ms.  

This is the data file where both the enum is declared and the operator overloaded.

/*
 * categoria.h
 *
 *  Created on: 9 de Out de 2010
 *      Author: Francisco
 */

#ifndef CATEGORIA_H_
#define CATEGORIA_H_

#include <iostream>

enum Categoria {
    LongoCurso,
    MedioCurso,
    Domestico
};

std::ostream& operator<<(std::ostream & os, Categoria & cat)
{
  switch (cat) {
  case LongoCurso:
      os << "Longo Curso";
      break;
  case MedioCurso:
      os << "Medio Curso";
      break;
  case Domestico:
      os << "Domestico";
  }
  return os;
}



#endif /* CATEGORIA_H_ */

EDIT: I tried const reference, const by value and non const value. None compiled.

+3  A: 

Look at your function:

std::ostream& operator<<(std::ostream & os, Categoria & cat) 

It can only match a writable enumeration variable... make the type Categoria or const Categoria&.

Tony
Shameless plug: [How to pass objects to functions in C++?](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254)
sbi
Did not work, but thanks.
Francisco P.
I should have looked more carefully at your compiler errors... sorry! Kudos to Alf.
Tony
+1  A: 

Try like this:

std::ostream &operator<<( std::ostream &os, const Categoria obj );

EDIT

doh multiple definition. If you prefer to define a function in a header, then at least put inline :

inline
std::ostream &operator<<( std::ostream &os, const Categoria cat )
{
  switch (cat) {
  case LongoCurso:
      os << "Longo Curso";
      break;
  case MedioCurso:
      os << "Medio Curso";
      break;
  case Domestico:
      os << "Domestico";
  }
  return os;
}
VJo
Did not work. Thanks anyway!
Francisco P.
Thank you, that worked, but I moved the implementation to a source file.
Francisco P.
+5  A: 

Multiple definition linker errors because you've defined the function in a header file that's included in two or more compilation units.

Either add inline, like

inline std::ostream& operator<<(std::ostream & os, Categoria & cat)

or move the definition to an implementation file (you still need a declaration in the header file then).

EDIT: PS: also, as others have mentioned (I didn't see that), pass the second argument by reference to const, like Categoria const& cat. Or if Categoriais an anum (darn, I can't see that here while editing), pass it by value.

EDIT 2: PPS: if you move the definition to an implementation file, then to reduce needless dependencies for client code, move also the #include <iostream> to the implementation file, and in the header file then #include <iosfwd>.

Cheers & hth.,

Alf P. Steinbach
Accepted, thanks.
Francisco P.
"darn, I can't see that here while editing": while editing there's a grey bar under the question title which you can drag down to reveal as much of the question text as necessary.
Eamon Nerbonne