【问题标题】:problem compiling with different gcc versions使用不同的 gcc 版本编译的问题
【发布时间】:2019-10-28 19:05:47
【问题描述】:

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 = (Entry)get_element(key);

source_file.cpp:82:34: 注意:依赖基中的声明 ->'HashSetBase' 未通过非限定查找找到

source_file.cpp:82:34: 注意:改用‘this->get_element’

【问题讨论】:

  • 试试这个->get_element
  • 感谢您的支持。我已经尝试过了,但没有成功。
  • 您应该添加一个最小的可重现示例。您的代码无法编译,因为例如方法 clear() 和值 empty_value 未声明。
  • 对不起,请从这个编译器中找到源代码:rextester.com/JENY4459
  • 或请参阅顶部。

标签: c++ type-conversion gcc4


【解决方案1】:

最后我通过修改这行代码解决了这个问题: this->get_element;

【讨论】:

  • 那么,我的建议是什么?
  • 谢谢,但第一次,它不起作用,可能是访问修饰符问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2015-01-16
  • 1970-01-01
  • 2019-07-08
相关资源
最近更新 更多