compiler error saying invalid initialization of reference of type somethingamp; from expression of type something*(编译器错误,提示对 somethingamp; 类型的引用初始化无效来自某物类型的表达*)
问题描述
我有一个像
test(something &)
我正在做
something *ss = new something();
我说
test(ss)
编译器抱怨说初始化了 something& 类型的引用from 表达 something * .
compiler complains saying initialization of reference of type something& from expression something * .
但不是 new 返回地址并且 ss 必须指向该地址!所以如果测试期待一个参考是不是它 ss 代表一个参考?
but isn't that new returns the address and ss must point to that address ! so if test is expecting a reference is not it ss represents a reference ?
推荐答案
你的函数需要一个普通的 something 对象.你不需要在这里使用指针:
Your function expects a normal something object. You don't need to use a pointer here:
something ss;
test(ss);
当您的函数签名看起来像 f(T&) 时,这意味着它接受对 T 对象的引用.当签名为f(T*)时,表示它接受一个指针到一个T对象.
When your function signature looks like f(T&), it means that it accepts a reference to a T object. When the signature is f(T*), it means that it accepts a pointer to a T object.
这篇关于编译器错误,提示对 something& 类型的引用初始化无效来自某物类型的表达*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:编译器错误,提示对 something& 类型的引用初始化无效来自某物类型的表达*
基础教程推荐
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
