Eclipse IDE 在“无法解析方法‘get’”这一行向我抛出错误。确切的错误信息是
Method 'get' could not be resolved, Location: line xxx, Type: Semantic Error
错误行:
const TagValue* tagValue = ((*mktDataOptions)[i]).get();
这里出了什么问题?这段代码是作为 API 提供给我的,所以想必其他人以前也用过它。类似的错误在我收到的代码中出现了大约 8 次,我很难相信它有 8 次以相同的方式编码错误。
问题
有人可以为我解析这一行并准确解释它试图做什么吗?
为什么我的 Eclipse 会针对所有这些实例抛出错误,如何修复?
相关代码
此行存在于函数中:
void EClient::reqMktData(TickerId tickerId, const Contract& contract,
const std::string& genericTicks, bool snapshot, const TagValueListSPtr& mktDataOptions)
...
if( m_serverVersion >= MIN_SERVER_VER_LINKING) {
std::string mktDataOptionsStr("");
const int mktDataOptionsCount = mktDataOptions.get() ? mktDataOptions->size() : 0;
...
const TagValue* tagValue = ((*mktDataOptions)[i]).get();
其中“TagValueListSPtr”在 header 中定义为
struct TagValue
{
TagValue() {}
TagValue(const std::string& p_tag, const std::string& p_value)
: tag(p_tag), value(p_value)
{}
std::string tag;
std::string value;
};
typedef shared_ptr<TagValue> TagValueSPtr;
typedef std::vector<TagValueSPtr> TagValueList;
typedef shared_ptr<TagValueList> TagValueListSPtr;
而“shared_ptr”定义为
template<typename X> class shared_ptr {
public:
typedef shared_ptr_defs::Use Use;
template<typename Y> friend class shared_ptr;
explicit shared_ptr(X* ptr = 0) : ptr_(ptr) {}
~shared_ptr() { if (use_.only()) delete ptr_; }
template<typename Y>
shared_ptr(const shared_ptr<Y>& other)
: ptr_(other.ptr_),
use_(other.use_)
{}
shared_ptr& operator=(const shared_ptr& other) {
if ( &use_ == &other.use_ ) { return *this; }
if ( use_.only() ) { delete ptr_; }
use_ = other.use_;
ptr_ = other.ptr_;
return *this;
}
X& operator*() const { return *ptr_; }
X* operator->() const { return ptr_; }
X* get() const { return ptr_; }
bool only() const { return use_.only(); }
void reset(X* ptr = 0) {
if ( use_.only() ) { delete ptr_; }
ptr_ = ptr;
use_ = Use();
}
private:
X *ptr_;
Use use_;
};
最佳答案
Can someone parse this line for me and explain exactly what it is trying to do?
/* A pointer (to a const qualified TagValue) */
const TagValue*
/* named */ tagValue
/* is initialised to the result of */ = ((
/* dereferencing */ *
/* the shared pointer */ mktDataOptions)
/* which points to a vector */
/* and calling operator[] on it */ [i])
/* which returns the reference to */
/* a shared pointer to a TagValue */
/* whose */ .get();
/* member function is called, returning /*
/* as final result a pointer to a TagValue */
这实际上是一个非常复杂的设计,涉及所有权(涉及很多共享指针),这就是为什么我怀疑这是否真的需要,但是很好。
mktDataOptions
是指向 vector 的共享指针,该 vector 包含指向 TagValue
的共享指针。因此,要获得指向 TagValue
的指针,您需要使用 shared_ptr::operator*()
取消引用指向 vector 的指针,选择一个元素(i
th) 该 vector 使用它的 std::vector::operator[]()
。这是一个指向 TagValue
的共享指针,因此要获取底层指针,应用成员函数 shared_ptr::get()
。
您可以将其重写为...
// using references, otherwise you make copies,
// which changes the meaning of the code.
// Get the vector from the shared pointer
std::vector<shared_ptr<TagValue>> & the_vector = *mktDataOptions;
// get a single element from it
shared_ptr<TagValue> const & the_element = the_vector[i];
// get it's underlying pointer
TagValue * the_pointer = the_element.get();
// it's a pointer to a non const TagValue, which can be
// converted to a pointer to a const TagValue (the type of the variable
// which gets initialised)
TagValue const * tagValue = the_pointer;
Why is my Eclipse throwing an error for all these instances and how can it be fixed?
[...] Is this an Eclipse error I should feel free to ignore? I'd still like to get rid of it.
根据一些研究,它似乎是一个 eclipse 错误,可以通过删除 workspace/.metadata
来解决,但我需要注意我不是 eclipse 用户,所以我无法验证或推荐一些可能的解决方案。
在您的情况下,我会制作工作区和项目目录的拷贝,然后开始尝试来自以下方面的一些建议:
- Eclipse CDT shows semantic errors, but compilation is ok
- Eclipse compiles successfully but still gives semantic errors
但从这些问题来看,这似乎是一个比较奇怪的错误,所以要做好受挫的准备。
关于c++ - 在此示例中,在对类的引用之前使用 * 运算符是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931171/