c++ - 没有可用于结构 vector 的成员

标签 c++ class templates vector struct

我有一个名为Heap 的模板类。我的问题是我想在底部实现插入功能。但是我无法访问 Values vector 的成员变量。当我写 Values[hole/2].tall 时,它无法到达它(tall 变量不会在写点后直接出现)。那么有人可以解释一下吗?

template <class Comparable>
class Heap
{
...
private:
// I have this vector
vector <heightNode <Comparable>> Values

};

template <typename Comparable>
struct heightNode
{
    Comparable tall;
    Comparable label;

    heightNode(const Comparable & t = Comparable(), const Comparable & la = Comparable()): tall(t),label(la) {}

    heightNode(const heightNode & rhs): tall(rhs.tall),label(rhs.label){}
};


template <class Comparable>
void Heap <Comparable>:: insert (const Comparable & value, const int & label)
{
    if(isFull())
    {
        cout <<"Heap is Full";
    }
    else
    {
        ++currentSize;//hole is an index we will put it to the last point in the vector.
        for(int hole=currentSize; hole>1 && (value > Values[hole/2].tall)); hole/=2 )
        {

        }
    }
}

最佳答案

这个

for(int hole=currentSize; hole>1 && (value > Values[hole/2].tall)); hole/=2 )

包含一个多余的圆括号;做到这一点

for (int hole=currentSize; hole>1 && (value > Values[hole/2].tall); hole/=2)

关于c++ - 没有可用于结构 vector 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34015941/

相关文章:

javascript - 在类javascript类构造函数中使用异步调用

c++ - 成员函数特征

c++ - 完美转发/移动构造不适用于 std::apply 中的元组

c++ - 从另一个文件访问 C++ 中的 extern "C"变量

c++ - 如何在exe文件夹中不包含纹理

c++ - 有没有什么方法可以在用户输入文本时读取字符?

c++ - 在 C++ 中优化 if-else 分支的一个小循环

c# - 设置对象成员时出现 NullReferenceException

c# - 在构造函数中或在类的顶部创建一个对象

c++ - 模板类的静态模板成员函数