尝试使用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类型进行操作时遇到了问题。不过,这应该不成问题,因为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;
这会引发编译错误。
header
关于c++ - Poco::AutoPtr上的const指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47015430/