使用不同的gcc版本进行编译时出现问题

时间:2019-10-28 19:05:47

标签: c++ type-conversion gcc4

gcc版本4.4.6成功编译代码。但是gcc版本4.8.1给出了编译错误。

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

class AbstractHashSet
{
    protected :
    int size;
    float load_factor;
    int current_limit;
    public :
    AbstractHashSet() : size(0), load_factor(0.75), current_limit(0){}
    int getSize(void) const { return size; }
    bool isEmpty(void) const { return size == 0; }
    float getLoadFactor(void) const {   return load_factor; }
    int getCapacity(void) const { return current_limit; }
    static int get_preferred_size(int n);
};

template<class KEY>
struct HashSetBaseElement
{
    KEY key;
    HashSetBaseElement *next;
    HashSetBaseElement(KEY k) : key(k),next(NULL){}
    virtual ~HashSetBaseElement(){}
};

template<class KEY>
class HashSetBase : public AbstractHashSet
{
    public :
    bool contains(KEY key)
    {
        if (size == 0) return false;
        return get_element(key) != NULL;
    }
    protected :
    HashSetBaseElement<KEY> *get_element(KEY key)
    {
        return NULL;
    }
};

template<class KEY>
class HashSet : public HashSetBase<KEY>
{
    public :
    HashSet(int n = 10)
    {
        HashSetBase<KEY>::setCapacity(n);
    }
    bool add(KEY key)
    {
        HashSetBaseElement<KEY> *e = get_element(key);
        if (e) return false;
        e =  new HashSetBaseElement<KEY>(key);
        return true;
    }
};
template<class KEY_T, class VALUE_T>
class HashMap : public HashSetBase<KEY_T>
{
    protected :
    struct Entry : public HashSetBaseElement<KEY_T>
    {
        VALUE_T value;
        Entry(KEY_T key, VALUE_T value) : HashSetBaseElement<KEY_T>(key), value(value){}
    };
    VALUE_T empty_value;

    public:
    VALUE_T &get(KEY_T key)
        {
            Entry *e = (Entry*)get_element(key);
            if (e) return e->value;
            return empty_value;
        }       
};

    struct Space
        {
            friend bool operator<(const Space &a, const Space &b)   
            {   return true;    }
            friend bool operator>(const Space &a, const Space &b)   
            {   return true;    }
        };
int main()
{    
    HashMap<int,Space*> pos_map;
    pos_map.get(100);
    cout<<"Hello World";

    return 0;
}

编译错误:

错误:source_file.cpp:实例化VALUE_T&HashMap :: get(KEY_T)[with KEY_T = int; VALUE_T = Space *]’:source_file.cpp:98:20:从此处开始必填

source_file.cpp:82:34:错误:在此作用域中未声明“ get_element”,并且在实例化[-fpermissive]时,依赖于参数的查找未找到任何声明

条目 e =(条目)get_element(key);

source_file.cpp:82:34:注意:不合格的查找找不到依赖库->“ HashSetBase”中的声明

source_file.cpp:82:34:注意:请改用“ this-> get_element”

1 个答案:

答案 0 :(得分:0)

最后,我通过修改以下代码来解决这个问题: this-> get_element;