c++ - 不能为 C++ 中的枚举重载 <<

标签 c++ enums operator-overloading compiler-errors

我创建了一个枚举数据类型来定义可能的飞行长度。我想重载它的 << 运算符,以便表示更好。

当我编译它时,我收到以下错误(为完整起见而发布。基本上是 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.  

这是声明枚举和重载运算符的数据文件。

/*
 * 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_ */

编辑: 我尝试了 const 引用、const 按值和非 const 值。没有编译。

最佳答案

多个定义链接器错误,因为您在包含在两个或多个编译单元中的头文件中定义了函数。

或者添加inline , 喜欢

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

或者将定义移动到一个实现文件(那时你仍然需要在头文件中声明)。

编辑:PS:另外,正如其他人提到的(我没看到),通过引用传递第二个参数 const , 比如 Categoria const& cat .或者如果 Categoria是枚举,按值传递。

编辑 2:PPS:如果您将定义移动到一个实现文件,那么为了减少对客户端代码不必要的依赖,也移动 #include <iostream>到实现文件,然后在头文件中 #include <iosfwd> .

关于c++ - 不能为 C++ 中的枚举重载 <<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4013703/

相关文章:

enums - 从 Go 中的一组相关常量创建一个枚举

c# - 通过字符串获取枚举int值

c# - 隐式转换运算符和相等运算符

C++ - 重载 += 运算符

c++ - Unix 用户空间中的实时 IPC

c++ - 分段故障打印文件特征

swift - "static"对枚举或结构中的常量意味着什么

c++ - 无法从函数返回结构

c++ - double free or corruption (out) and Aborted (core dumped) 错误

c++ - 将一个对象与左侧的常数相乘