内联函数的 C++ 作用域

标签 c++ inline

我遇到了编译错误:

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/

相关文章:

c++ - 如何使 `std::is_empty_v<T> && sizeof(T) > 1` 为真的类型 T?

c++ - C 和 C++ 类型之间的区别 (typedef)

java - Google ProtoBuf 序列化/反序列化

c++ - libQGLViewer如何在不清除缓冲区的情况下绘制

css - Joomla 中的内联 CSS

c++ - 未命中内联函数中的断点

inline - 仅用于一个文件或一个函数的 doxygen 内联源

c++ - 如何从多个对象访问同一个资源?

c++ - 如何在 C++ 中定义内联自由函数(非成员函数)?

c - GCC 会内联一个接受指针的函数吗?