F. *_* P. 2 c++ enums compiler-errors operator-overloading
我创建了一个枚举数据类型来定义可能的航班长度.我想重载它的<<运算符,所以表示更好.
当我编译它时,我得到以下错误(为了完整性而发布.基本上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.
Run Code Online (Sandbox Code Playgroud)
这是声明枚举和运算符重载的数据文件.
/*
* 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_ */
Run Code Online (Sandbox Code Playgroud)
编辑: 我尝试了const引用,const值和非const值.没有编译.
Che*_*Alf 16
多定义链接器错误,因为您已在两个或多个编译单元中包含的头文件中定义了该函数.
要么添加inline,就像
inline std::ostream& operator<<(std::ostream & os, Categoria & cat)
Run Code Online (Sandbox Code Playgroud)
或者将定义移动到实现文件(然后您仍然需要在头文件中声明).
编辑:PS:另外,正如其他人所提到的(我没有看到),通过参考传递第二个参数const,就像Categoria const& cat.或者如果Categoria是枚举,则按值传递.
编辑2:PPS:如果您将定义移动到实现文件,然后为了减少客户端代码的不必要的依赖性,还可以移动到实现文件,然后移动#include <iostream>到头文件中#include <iosfwd>.
| 归档时间: |
|
| 查看次数: |
8088 次 |
| 最近记录: |