【问题标题】:Can't overload << for Enum in C++不能为 C++ 中的枚举重载 <<
【发布时间】:2010-10-25 10:34:30
【问题描述】:

我创建了一个枚举数据类型来定义可能的飞行长度。我想重载它的

当我编译这个时,我收到以下错误(为了完整性而发布。基本上是multiple definitions of operator &lt;&lt;(ostream&amp;, Categoria&amp;)):

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

编辑: 我尝试了常量引用、常量值和非常量值。未编译。

【问题讨论】:

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


    【解决方案1】:

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

    要么加inline,点赞

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

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

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

    编辑 2:PPS:如果您将定义移动到实现文件中,那么为了减少对客户端代码的不必要依赖,还将 #include &lt;iostream&gt; 移动到实现文件和头文件中然后#include &lt;iosfwd&gt;

    【讨论】:

    • “该死,我在编辑时看不到”:在编辑时,问题标题下方有一个灰色条,您可以向下拖动以显示尽可能多的问题文本。跨度>
    • @Cheersandhth "anum" 在这个答案中应该是 "enum"。我没有足够的代表来进行这么小的编辑。
    【解决方案2】:

    看看你的功能:

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

    它只能匹配一个可写的枚举变量...将类型设为Categoriaconst Categoria&amp;

    【讨论】:

    【解决方案3】:

    试试这样:

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

    编辑

    doh 多重定义。如果你喜欢在头文件中定义一个函数,那么至少把 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;
    }
    

    【讨论】:

    • 谢谢,这行得通,但我将实现移到了源文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2017-12-28
    • 2010-11-24
    • 1970-01-01
    • 2018-10-25
    • 2010-11-26
    相关资源
    最近更新 更多