gpt4 book ai didi

c++ - Poco::AutoPtr 上的常量指针

转载 作者:行者123 更新时间:2023-11-28 04:56:30 25 4
gpt4 key购买 nike

尝试使用 AutoPtr 处理 const 指针:

void LiveTimeConfig::setup(const AbstractConfiguration& cfg){
GetLogger().information("LiveTimeConfig::setup(): init data");
Poco::AutoPtr<const AbstractConfiguration> view =
cfg.createView("live_time");
try {
readExist(view, Field::Token, _token);
readExist(view, Field::Solt, _solt);
} catch (Poco::SyntaxException& ) {
GetLogger().error("LiveTimeConfig::setup(): bad values");
throw Poco::RuntimeException("LiveTimeConfig::setup(): invalid format");
}
}

但是我在比较运算符上有错误。

../../../build/pc/include/Poco/AutoPtr.h: In instantiation of ‘class Poco::AutoPtr<const Poco::Util::AbstractConfiguration>’: src/LiveTimeConfig.cpp:27:54:   required from here ../../../../build/pc/include/Poco/AutoPtr.h:263:7: error: ‘bool Poco::AutoPtr<C>::operator==(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator == (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:258:7: error: with ‘bool Poco::AutoPtr<C>::operator==(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator == (const C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:278:7: error: ‘bool Poco::AutoPtr<C>::operator!=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded bool operator != (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:273:7: error: with ‘bool Poco::AutoPtr<C>::operator!=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator != (const C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:293:7: error: ‘bool Poco::AutoPtr<C>::operator<(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded bool operator < (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:288:7: error: with ‘bool Poco::AutoPtr<C>::operator<(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator < (const C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:308:7: error: ‘bool Poco::AutoPtr<C>::operator<=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded bool operator <= (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:303:7: error: with ‘bool Poco::AutoPtr<C>::operator<=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator <= (const C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:323:7: error: ‘bool Poco::AutoPtr<C>::operator>(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded bool operator > (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:318:7: error: with ‘bool Poco::AutoPtr<C>::operator>(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator > (const C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:338:7: error: ‘bool Poco::AutoPtr<C>::operator>=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded bool operator >= (C* ptr) const
^ ../../../../build/pc/include/Poco/AutoPtr.h:333:7: error: with ‘bool Poco::AutoPtr<C>::operator>=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’ bool operator >= (const C* ptr) const

如何为 const AbstractConfiguration* 创建 AutoPtr

UPD

如果使用

const Poco::AutoPtr<AbstractConfiguration> view = cfg.createView("live_time");

错误是

/home/evgen/projects/AuthService/Core/Module/src/LiveTimeConfig.cpp:27: error: invalid conversion from ‘const Poco::Util::AbstractConfiguration*’ to ‘Poco::Util::AbstractConfiguration*’ [-fpermissive]
const Poco::AutoPtr<AbstractConfiguration> view = cfg.createView("live_time");
^

最佳答案

我不知道这个库,也不知道它的内部结构,但它似乎在内部对 const 类型进行操作时遇到了问题。不过这应该不是问题,因为来自 header , get函数有两个重载,一个用于 const对象实例返回 const指针。 ( const C * get() const; ) 所以你可以使用:

const Poco::AutoPtr<AbstractConfiguration> view = 
cfg.createView("live_time");

作为指向 const AbstractConfiguration 的指针.

编辑:

这个编译器错误的原因也可以从这个头文件中读取。因此,AutoPtr 类为每个运算符声明了一对函数 ==等等……

bool operator == (
const C * ptr
) const;

bool operator == (
C * ptr
) const;

但是当你声明 Poco::AutoPtr<const AbstractConfiguration>两个重载具有相同的声明

bool operator == (
const AbstractConfiguration * ptr
) const;

这会引发编译错误。

Minimal example

关于c++ - Poco::AutoPtr 上的常量指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47015430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com