[英]Function returning <error-type> instead of the intended raw pointer to a template class in C++
I am having trouble using the first_
variable in my template class (shown below).我在模板 class 中使用first_
变量时遇到问题(如下所示)。
template<typename T> class enable_movable_ptr {
public:
//default constructor
enable_movable_ptr() : ptr_(nullptr) {};
enable_movable_ptr(T* p) : ptr_(p) {};
//...
//other constructors and operators
//...
T* get() {return ptr_; };
movable_ptr<T>* First() { return first_; };
private:
T* ptr_;
movable_ptr<T>* first_ = nullptr;
};
template<typename T> class movable_ptr {
public:
//Parameterless constructor
movable_ptr() : trg_(nullptr) {};
//Constructor from T*
movable_ptr(T* p) : trg_(p) { add_to_tracked(this); };
//...
//other constructors and operators
//...
//access to variables
enable_movable_ptr<T>* get() {return trg_; };
movable_ptr<T>* Next(enable_movable_ptr<T>& p) {return next_; };
movable_ptr<T>* Previous(enable_movable_ptr<T>& p) {return prev_; };
//get_movable
movable_ptr<T>* get_movable(enable_movable_ptr<T>& p) {};
private:
enable_movable_ptr<T>* trg_;
movable_ptr<T>* next_ = nullptr;
movable_ptr<T>* prev_ = nullptr;
};
template<typename T> movable_ptr<T> get_movable(enable_movable_ptr<T>& p){
if (p.First() != nullptr)
{}
};
The problem is, the returned first_
is of the <error-type>
type (that's what it shows if I mouse over any first_
or First()
in the code, except for the declaration in VS2019), instead of the expected movable_ptr<T>*
.问题是,返回的first_
是<error-type>
类型(如果我将鼠标悬停在代码中的任何first_
或First()
上,它就会显示,VS2019 中的声明除外),而不是预期的movable_ptr<T>*
。 However, if I mouse over the declaration of first_
, it shows the correct movable_ptr<T>*
type.但是,如果我将鼠标悬停在first_
的声明上,它会显示正确的movable_ptr<T>*
类型。
From my point of view it seems as if the compiler has the memory of a goldfish, and forgets what the type is even while still within the same class definition.从我的角度来看,似乎编译器具有金鱼的 memory,并且即使仍在相同的 class 定义中,也忘记了该类型是什么。 That is most likely not the case, and I suspect there is something I might have done wrong which causes this.很可能不是这种情况,我怀疑我可能做错了什么导致了这种情况。
Any ideas on where I went wrong, or how to solve the problem?关于我哪里出错或如何解决问题的任何想法?
Just forward declare movable_ptr
before defining enable_movable_ptr
.只需在定义enable_movable_ptr
之前前向声明movable_ptr
。
template<typename T> class movable_ptr;
Otherwise, the compiler doesn't know that movable_ptr
is even a thing that might exist.否则,编译器不知道movable_ptr
甚至是可能存在的东西。
Also, it's better to not rely on external tooling, such as your IDE, to tell you what is wrong with the code.此外,最好不要依赖外部工具,例如您的 IDE,来告诉您代码有什么问题。 Let the compiler do that;让编译器这样做; it very likely knows much more about your code.它很可能对您的代码了解更多。
Here's where the issue actually comes from (I've removed all the code that is irrelevant to your particular issue)这就是问题的真正来源(我已经删除了所有与您的特定问题无关的代码)
// you need to forward declare
template <typename T> struct movable_ptr;
template <typename T> struct enable_movable_ptr {
movable_ptr<T>* first_ = nullptr; // else compiler won't know what movable_ptr is
};
template <typename T> struct movable_ptr {
enable_movable_ptr<T>* trg_;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.