c++ - c ++链接错误与类中的const成员函数
问题描述
我有一个类似的声明
class ABC_EXPORT abc
{
....
public:
xyz foo1(const arg1 a, const arg3 b) const; //defined in cpp file
xyz foo2(const arg1 a, const arg2 b) const { return foo1(a, convert_to_arg3(b));}
};
这被内置到一个 DLL 库中,另一个项目调用foo2
. 我收到链接器错误 LNK2019 : unresolved external symbol。但是,如果我const
从链接的定义中删除,foo2
则解决。有人可以解释一下这里发生了什么吗?我在 Windows 7 上使用 VS2012
解决方案
确保声明的函数签名和定义匹配。一个常见的错误是忘记const
函数定义上的说明符。例如,
struct A
{
void do_foo_const(int a, int b) const;
};
// An incorrect definition.
// Notice the missing 'const' here V
void A::do_foo_const(int a, int b)
{
...
}
// correct definition.
void A::do_foo_const(int a, int b) const
{
...
}
这种分离允许函数的 const 版本和非 const 版本彼此并存,并返回不同的结果。C++ 标准库中使用此功能的一个示例是std::vector<T>::at
. 请注意 const 版本如何返回一个 const 引用,但非 const 版本返回一个非常量引用。
推荐阅读
- javascript - 将具有数组值的对象转换为另一个对象数组
- go - Golang 将 UTF-8 字符串转换为 ASCII
- sql - 获取过去 2 周在任何给定日期发生的事件
- sql-server - 存储过程中的服务器主体错误,同时忽略 USE 中指定的数据库
- node.js - 为什么我不能安装 Angular cli?npm install -g @angular/cli 不安装
- c# - 将数据网格复选框中的值更新或插入到数据库中
- google-chrome - 如何使 Chrome 扩展在从 WebStorm 打开的 Chrome 实例中可用?
- htmx - 处理 htmxjs 中的连接错误
- python-3.x - 如何仅更改 ttk.Entry 的一侧边框宽度?
- excel - 数组公式不在 Excel MAC 2020 上计算