不能超载<<对于 C++ 中的枚举
我创建了一个枚举数据类型来定义可能的飞行长度。我想超载它的 <<运算符,因此表示效果更好。
当我编译这个时,我收到以下错误(为了完整性而发布。基本上运算符的多个定义<<(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。没有编译。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看你的函数:
它只能匹配可写的枚举变量...使类型为
Categoria
或const Categoria&
。Look at your function:
It can only match a writable enumeration variable... make the type
Categoria
orconst Categoria&
.尝试这样:
编辑
多重定义。如果您喜欢在标头中定义函数,那么至少放置 inline :
Try like this:
EDIT
doh multiple definition. If you prefer to define a function in a header, then at least put inline :
多个定义链接器错误,因为您在包含在两个或多个编译单元中的头文件中定义了该函数。
添加
内联
,或者将定义移动到实现文件(那么您仍然需要在头文件中声明)。
编辑: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
, likeor 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
, likeCategoria const& cat
. Or ifCategoria
is 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>
.