不理解 C++ 类型不匹配:const Foo* 到 Foo* const&
Not understanding C++ type mismatch: const Foo* to Foo* const&
拥有这组对象和语句:
QSet<Foo*> set;
iterator QSet::insert(const T & value) //type of the function I want to call
const Foo * get() const //type of the function I use to get the argument
set.insert(get()); //the line showing up as error
我收到错误 "no known conversion for argument 1 from 'const Foo*' to 'Foo* const&"。我想我在阅读这些类型时遇到了麻烦,因为我不知道我应该怎么做才能完成这项工作。
根据我的阅读,const 关键字适用于其左侧的类型,但顶级 const 除外,它可以写在它所适用的类型的左侧。我的猜测是我必须将 get() 转换为引用,但我不确定该怎么做。
您正在尝试将 const Foo *
插入 QSet<Foo *>
,但编译器不会自动将 const Foo *
转换为普通的 Foo *
为了这样做。
我通过如下更改集合的类型解决了这个问题。
QSet<Foo const*> set;
修改后编译成功。
您违反了 return 从您的 get()
函数中编辑的指针的常量性。 get()
return 是一个指向 Foo
类型的常量对象的指针,但随后您尝试将其插入到非常量 Foo
指针的向量中。您需要 const_cast
来自 get()
的 return 值或将 get()
的 return 类型从 const Foo*
更改为仅 Foo*
。
这里似乎有一些误解(提问者和一些答案)。
先,你说"My guess would be that I have to convert get()
to a reference but I'm unsure how to do that"。让我们尝试清除它:
1) "I have to convert get()
to a reference" -- 事实上,你没有!
iterator QSet::insert(const T & value)
确实有借鉴意义。但它是对类型 T
的引用。所以问题是,"what is type T
"?
在这种情况下,T=Foo *
。所以 insert
,在模板的这个实例中,实际上是:
iterator QSet::insert(Foo * const & value)
-- 从右到左阅读:insert
引用指向 Foo
.
的常量指针
2) "I'm unsure how to do that [convert a pointer to a reference]" -- 虽然您不必在此处执行此操作,但通常您可以通过取消引用 get 的结果来执行此操作。例如:*(get())
.
其次,编译错误。出现这个错误是因为有冲突:
a) get()
returns a const Foo *
b) 但 set
存储 Foo*
-- 不是 const Foo *
,因此 insert
仅接受可更改的 Foo
所以你不能在你的 QSet<Foo*>
中存储一个常量指针。这是有道理的,因为您可以使用 set
访问和更改其中的 Foo
s,您保证不会使用 const Foo
.
此参考资料应该会有帮助:
https://isocpp.org/wiki/faq/const-correctness
您可能还想是否可以只使用 QSet<Foo>
而不是 QSet<Foo*>
。在前一种情况下,事情可能会按照您的预期进行。
拥有这组对象和语句:
QSet<Foo*> set;
iterator QSet::insert(const T & value) //type of the function I want to call
const Foo * get() const //type of the function I use to get the argument
set.insert(get()); //the line showing up as error
我收到错误 "no known conversion for argument 1 from 'const Foo*' to 'Foo* const&"。我想我在阅读这些类型时遇到了麻烦,因为我不知道我应该怎么做才能完成这项工作。
根据我的阅读,const 关键字适用于其左侧的类型,但顶级 const 除外,它可以写在它所适用的类型的左侧。我的猜测是我必须将 get() 转换为引用,但我不确定该怎么做。
您正在尝试将 const Foo *
插入 QSet<Foo *>
,但编译器不会自动将 const Foo *
转换为普通的 Foo *
为了这样做。
我通过如下更改集合的类型解决了这个问题。
QSet<Foo const*> set;
修改后编译成功。
您违反了 return 从您的 get()
函数中编辑的指针的常量性。 get()
return 是一个指向 Foo
类型的常量对象的指针,但随后您尝试将其插入到非常量 Foo
指针的向量中。您需要 const_cast
来自 get()
的 return 值或将 get()
的 return 类型从 const Foo*
更改为仅 Foo*
。
这里似乎有一些误解(提问者和一些答案)。
先,你说"My guess would be that I have to convert get()
to a reference but I'm unsure how to do that"。让我们尝试清除它:
1) "I have to convert get()
to a reference" -- 事实上,你没有!
iterator QSet::insert(const T & value)
确实有借鉴意义。但它是对类型 T
的引用。所以问题是,"what is type T
"?
在这种情况下,T=Foo *
。所以 insert
,在模板的这个实例中,实际上是:
iterator QSet::insert(Foo * const & value)
-- 从右到左阅读:insert
引用指向 Foo
.
2) "I'm unsure how to do that [convert a pointer to a reference]" -- 虽然您不必在此处执行此操作,但通常您可以通过取消引用 get 的结果来执行此操作。例如:*(get())
.
其次,编译错误。出现这个错误是因为有冲突:
a) get()
returns a const Foo *
b) 但 set
存储 Foo*
-- 不是 const Foo *
,因此 insert
仅接受可更改的 Foo
所以你不能在你的 QSet<Foo*>
中存储一个常量指针。这是有道理的,因为您可以使用 set
访问和更改其中的 Foo
s,您保证不会使用 const Foo
.
此参考资料应该会有帮助:
https://isocpp.org/wiki/faq/const-correctness
您可能还想是否可以只使用 QSet<Foo>
而不是 QSet<Foo*>
。在前一种情况下,事情可能会按照您的预期进行。