gpt4 book ai didi

c++ - 错误 : invalid use of 'Config::testMap'

转载 作者:行者123 更新时间:2023-11-28 03:48:03 25 4
gpt4 key购买 nike

代码如下:

#include <iostream>
#include <string>
#include <map>
#include <stdexcept>
#include <boost/ptr_container/ptr_vector.hpp>

struct TestStruct
{
std::string str1;
int var1;
};

struct Config
{
// Map
typedef std::map< std::string, boost::ptr_vector<struct TestStruct> > testMap;
};

void foo(Config& config)
{
if (config.testMap.empty())
{
std::cout << "It worked!" << std::endl;
}
else
{
std::cout << "It didn't work!" << std::endl;
}
return;
}

int testMain(void)
{
Config config;

foo(config);

return;
}

int main(void)
{
try
{
return testMain(/*argc, argv*/);
}
catch(std::exception& err)
{
std::cerr << "Error running program: " << err.what() << std::endl;
return 1;
}
catch(...)
{
std::cerr << "Program failed with an unknown exception." << std::endl;
return 1;
}
}

我是 map 新手 - 以前从未使用过。我发现了很多关于如何在线使用它们的例子。不幸的是,我似乎无法理解它们的更高级示例。

我想做的是用键 (std::string) 和值 (boost::ptr_vector<struct>) 创建一个映射。

我打算首先声明并成功传递它。然后我想尝试弄清楚如何填充它。

我遇到了一个非常模糊的错误,我不知道如何解释它。

关于我在“使用”testMap 时做错了什么的任何建议?

此外,有人可以提供一些简单的示例来说明我如何填充 map 。

假设我想要一个 a 的 key 值 str1 = "hello", var1 = 10 .我该怎么做?

跟进问题:关于 Kerrek SB 在下面留下的答案。

如果我执行以下...

std::string key   = "a";
TestStruct value = {"hello", 10};
config.testMap[key] = value;

我收到以下错误:

 error: no match for 'operator=' in 'config->Config::testMap.std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> > > >](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& key)))) = value'
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: candidates are: boost::ptr_vector<T, CloneAllocator, Allocator>& boost::ptr_vector<T, CloneAllocator, Allocator>::operator=(std::auto_ptr<boost::ptr_vector<T, CloneAllocator, Allocator> >) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: boost::ptr_vector<T, CloneAllocator, Allocator>& boost::ptr_vector<T, CloneAllocator, Allocator>::operator=(boost::ptr_vector<T, CloneAllocator, Allocator>) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]

如果我改为执行 .insert()方法我得到以下错误:

instantiated from here
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:106: error: no matching function for call to 'boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const TestStruct&)'
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:50: note: candidates are: boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::size_type, const typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::allocator_type&) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(std::auto_ptr<boost::ptr_vector<T, CloneAllocator, Allocator> >) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(const typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::allocator_type&) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector() [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:35: note: boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >&)

跟进:

根据我的研究,这是不可能的。

您不能使用 ptr_vector在 map 中。 (有人告诉我)由于所有权/拷贝问题?

“当你试图复制一个 ptr_vector 时会发生什么,这必然会发生在 map 内部?指针容器模型指针的独占所有权。

你可以用 C++0x 和 std::move 做到这一点,但这对你没有帮助。”

谁能举个反例?

最佳答案

您的类 Config 不包含任何成员! (只是一个 typedef。)也定义一个成员:

struct Config
{
typedef std::map<std::string, boost::ptr_vector<TestStruct> > testMap_type;

testMap_type testMap; // member
};

此外,在 C++ 中不需要说 struct TestStruct,只需说 TestStruct

添加一个元素:

std::string key = "xxx";
TestStruct val = { "hello", 10 };

// Insert with []-operator
config.testMap[key] = val;

// Insert with insert():
config.testMap.insert(std::pair<std::string, TestStruct>(key, val)); // alternative

编辑:抱歉,我歪曲了您的实际数据结构。下面是一个带有 std::vector 的示例:

typedef std::map<std::string, std::vector<int>> MyMap;
MyMap m;

m["hello"].push_back(1);
m["hello"].push_back(2);
m["world"].push_back(3);

在你的例子中,你可以说 config.testMap[key].push_back(val),或者你可以创建一个新的 vector :

boost::ptr_vector<TestStruct> new_v;
// populate new_v;
config.testMap.insert(std::pair<std::string, boost::ptr_vector<TestStruct>>(key, new_v);

关于c++ - 错误 : invalid use of 'Config::testMap' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794670/

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