C ++:头文件中全局函数的多重定义错误 [英] C++: Multiple definition error for global functions in the header file

查看:1327
本文介绍了C ++:头文件中全局函数的多重定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数是全局的,并在头文件中定义(暂时我想保留它)。



头文件构成一个具有内联函数和其中一个函数调用全局函数的特定类。



源文件不包含任何出现的



我可以发布代码if任何人都有兴趣。

  mainwindow.o:在函数`tileForCoordinate(double,double,int)':
mainwindow .cpp :(。text + 0x310):`tileForCoordinate(double,double,int)'的多重定义
main.o:main.cpp :( .text + 0xd0):首先定义这里
moc_mainwindow .o:在函数`qHash(QPoint const&)'中:
moc_mainwindow.cpp :(。text + 0x0):`qHash(QPoint const&)'的多重定义
main.o:main。 cpp :( .text + 0x0):首先在这里定义
moc_mainwindow.o:在函数`tileForCoordinate(double,double,int)'中:
moc_mainwindow.cpp :(。text + 0x150):multiple definition of'tileForCoordinate(double,double,int)'
main.o:main.cpp :( .text + 0xd0):首先定义这里
collect2:ld返回1退出状态
make: *** [SimpleRouting]错误1


解决方案

inline

  inline void globalfunc(){
}

虽然这样做意味着它不再严格是全局的 - 单位,但是链接器不会反对这个。


This function is global and is defined in the header file (temporarily I want to keep it there).

The header file also constitutes a particular class which has inline functions and one of those functions call this global function.

The source file doesn't contain any occurrences of the global function in question.

Any hints on cause of the error?

I can post the code if anyone is interested.

mainwindow.o: In function `tileForCoordinate(double, double, int)':
mainwindow.cpp:(.text+0x310): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
moc_mainwindow.o: In function `qHash(QPoint const&)':
moc_mainwindow.cpp:(.text+0x0): multiple definition of `qHash(QPoint const&)'
main.o:main.cpp:(.text+0x0): first defined here
moc_mainwindow.o: In function `tileForCoordinate(double, double, int)':
moc_mainwindow.cpp:(.text+0x150): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
collect2: ld returned 1 exit status
make: *** [SimpleRouting] Error 1

解决方案

mark it as inline:

 inline void globalfunc() { 
 }

although doing so means that it will no longer strictly be global - you will get a copy in each translation unit that uses the header, but the linker won't object to this.

这篇关于C ++:头文件中全局函数的多重定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆