不能超载<<对于 C++ 中的枚举

发布于 2024-09-29 06:39:18 字数 3407 浏览 3 评论 0 原文

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

当我编译这个时,我收到以下错误(为了完整性而发布。基本上运算符的多个定义<<(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 by value 和非 const value。没有编译。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青朷 2024-10-06 06:39:19

看看你的函数:

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

它只能匹配可写的枚举变量...使类型为 Categoriaconst Categoria&

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&.

痴情换悲伤 2024-10-06 06:39:19

尝试这样:

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

编辑

多重定义。如果您喜欢在标头中定义函数,那么至少放置 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;
}

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;
}
这个俗人 2024-10-06 06:39:18

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

添加内联

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

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

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

编辑2:PPS:如果将定义移动到实现文件,那么为了减少客户端代码不必要的依赖关系,请将 #include 移动到实现文件,然后在头文件中添加 #include

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 enum, 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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文