我遇到了编译错误:
Error 7 error C2084: function 'Boolean IsPointInRect(...)' already has a body
在我的内联函数上,它在 cpp 文件中是这样声明的:
inline Boolean IsPointInRect(...)
{
...
}
我在另一个cpp文件中有完全相同的功能。这可能是导致问题的原因吗?我该如何解决?
最佳答案
作为litb AndreyT 指出,此答案并未解决实际问题 - 有关详细信息,请参阅 litbs 答案。
虽然static
,如Ofir said ,为您提供内部链接,“C++ 方式” 是使用未命名的命名空间:
namespace
{
inline Boolean IsPointInRect(/*...*/) { /*...*/ }
}
§7.3.1.1/1:
An unnamed-namespace-definition behaves as if it were replaced by
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
where all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.
§7.3.1.1/2 添加:
The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative.
关于内联函数的 C++ 作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2378150/