【发布时间】:2015-03-12 16:47:36
【问题描述】:
这段代码用 VS 编译成功,但是移植到 Linux 并用 g++ 编译时出错。
template <class Key, class Value>
void Dictionary<Key, Value>::set(const Key &key, Value value)
{
typename Dictionary<Key,Value>::iterator i = this->find(key);
if (i == this->end())
{
access(&key, &value, eINSERT);
this->insert(value_type(key, value));
}
else
{
access(&key, &i->second, eCLEAR);
dispose(i->second);
access(&key, &value, eSET);
i->second = value;
}
}
template <class Key, class Value> class PtrDictionary : public Dictionary<Key, Value>;
class Processor
{
private:
ptrdictionary<const string,const type*> m_Types;
void Processor::add(const string name, const Type* t)
{
if (m_Types.get(name))
error("Type '"+string(name)+"' already defined", eParseError);
m_Types.set(name, t); // this is where the error is received
}
}
错误是:
../Packages/Dictionary.h: 在 'void Dictionary::set(const Key&, Value) [with Key = const 标准::基本字符串;值 = 常量 AsmLoader::Type*]': Instructions.cpp:94:21:从这里需要 ../Packages/Dictionary.h:79:37:错误:预期的主表达式
this->insert(value_type(key, value));
更新:
value_type 是 std::map 中的一个对象,g++ 编译代码所需要的只是添加这个:
typedef typename map<const Key, Value>::value_type value_type;
感谢大家的cmets,这是我第一次使用SO,这是一个非常复杂的代码,所以我忘了指定这部分......
【问题讨论】:
-
你试过
this->m_Types.set(name, t) -
我有,错误依旧...
-
这是完整的错误吗?
-
错误表示
set()函数中的this->insert(value_type(key, value));导致了问题。value_type()是什么? -
@ 0x499602D2 这是完整的错误。 @NathanOliver value_type() 是来自 std::map 的类型。
标签: c++ templates compiler-errors g++