gpt4 book ai didi

c++ - 在g++中收到的预期主表达式

转载 作者:行者123 更新时间:2023-12-02 11:10:43 25 4
gpt4 key购买 nike

该代码可通过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: In instantiation of 'void Dictionary::set(const Key&, Value) [with Key = const std::basic_string; Value = const AsmLoader::Type*]': Instructions.cpp:94:21: required from here ../Packages/Dictionary.h:79:37: error: expected primary-expression
this->insert(value_type(key, value));



更新:

value_type是std::map中的一个对象,而g++编译代码所需的只是添加以下代码:
typedef typename map<const Key, Value>::value_type value_type;

感谢大家的评论,这是我第一次使用SO,这是一个非常复杂的代码,所以我忘记指定这部分了。

最佳答案

不幸的是,它看起来好像不能以与VS中相同的方式实现。通过研究,我发现:

Can dictionaries be used in c++

他们建议在STL中使用std::map作为C++中的类似方法(不使用.NET)。如果以上链接发生变化,请引用以下提供的答案:

There is a corresponding type in STL, that's called std::map.

It has the same basic functionality as a .NET Dictionary, but the implementation is quite different. std::map is internally based on a red-black tree datastructure, while Dictionary uses a hash table internally.

If you're just looking for something with the same behaviour, std::map will do, but if you have large amounts of data you have to be aware of the different performance characteristics.



**警告-我仍在学习,如果这与您最初提出的问题不符,则深表歉意。希望这可以帮助!

关于c++ - 在g++中收到的预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com