无法在c ++中将元素插入集合中

时间:2011-12-26 06:09:39

标签: c++ stl set

我写了一个示例程序。下面:

#include<iostream>
#include<string>
#include<set>
using namespace std;

int main()
{

std::set<std::string> m;
m.insert("1-2-1-1");

return 0;
}

这完美无误。

但是我尝试创建一个集合并尝试在我的应用程序代码中插入elemts,它给了我一些错误。

        /export/SunStudio/SUNWspro/bin/CC -c -compat=5 -features=no%altspell -features=no%export -mt +d  -DTHREAD -DSOLARIS -staticlib=rwtools7 -library=rwtools7,iostream -DOWTOOLKIT_WARNING_DISABLED -Bsymbolic -PIC  -g0 -DBPDEBUG  -I.  -I../nls  -I../../cnacore/api  -I../../cnacore/external  -I../../cnacore/stl  -I../../cnabp/kernel  -I/vobs/oss_design/sab/inc  -I/vobs/oss_design/tbs/inc  -I/vobs/oss_design/tss/inc  -I/vobs/oss_design/tds/inc  -I/vobs/oss_design/eam/inc  -I/vobs/ossrc_3pp/cif_3pp/borland_enterprise_server/include  -I/vobs/ossrc_3pp/cif_3pp/borland_enterprise_server/include/stubs  -I/vobs/ossrc_build_dependencies/extracted_packages/ERICsnlm/include  -I/vobs/ossrc_3pp/cif_3pp/sybase_ase/OCS-15_0/include  -I/vobs/cna/src/features  cacup_bsc.cc
"cacup_bsc.cc", line 6545: Error: Multiple declaration for valid.
"cacup_bsc.cc", line 6545: Error: "," expected instead of ".".
"cacup_bsc.cc", line 6548: Error: Multiple declaration for valid.
"cacup_bsc.cc", line 6548: Error: "," expected instead of ".".
"cacup_bsc.cc", line 6552: Error: std::set_intersection<std::InputIterator1, std::InputIterator2, std::OutputIterator>(std::InputIterator1, std::InputIterator1, std::InputIterator2, std::InputIterator2, std::OutputIterator) is not a static data member.
"cacup_bsc.cc", line 6552: Error: Cannot use std::insert_iterator<std::set<std::string>> to initialize int.
"cacup_bsc.cc", line 8903: Warning: A non-POD object of type "bpDU" passed as a variable argument to function "std::sprintf(char*, const char*, ...)".
"cacup_bsc.cc", line 8906: Warning: A non-POD object of type "bpDU" passed as a variable argument to function "std::sprintf(char*, const char*, ...)".
6 Error(s) and 2 Warning(s) detected.
*** Error code 6
clearmake: Error: Build script failed for "cacup_bsc.o"

下面是我添加的代码。 但在评论下面的代码后,汇编成功了

/*
std::set<std::string> valid;
valid.insert(string("1-1"));

std::set<std::string> planned;
valid.insert(string("1-1"));

std::set<std::string> result;

std::set_intersection(valid.begin(), valid.end(), planned.begin(), planned.end(), std::inserter(result, result.end()));
*/

        /export/SunStudio/SUNWspro/bin/CC -c -compat=5 -features=no%altspell -features=no%export -mt +d  -DTHREAD -DSOLARIS -staticlib=rwtools7 -library=rwtools7,iostream -DOWTOOLKIT_WARNING_DISABLED -Bsymbolic -PIC  -g0 -DBPDEBUG  -I.  -I../nls  -I../../cnacore/api  -I../../cnacore/external  -I../../cnacore/stl  -I../../cnabp/kernel  -I/vobs/oss_design/sab/inc  -I/vobs/oss_design/tbs/inc  -I/vobs/oss_design/tss/inc  -I/vobs/oss_design/tds/inc  -I/vobs/oss_design/eam/inc  -I/vobs/ossrc_3pp/cif_3pp/borland_enterprise_server/include  -I/vobs/ossrc_3pp/cif_3pp/borland_enterprise_server/include/stubs  -I/vobs/ossrc_build_dependencies/extracted_packages/ERICsnlm/include  -I/vobs/ossrc_3pp/cif_3pp/sybase_ase/OCS-15_0/include  -I/vobs/cna/src/features  cacup_bsc.cc
"cacup_bsc.cc", line 8902: Warning: A non-POD object of type "bpDU" passed as a variable argument to function "std::sprintf(char*, const char*, ...)".
"cacup_bsc.cc", line 8905: Warning: A non-POD object of type "bpDU" passed as a variable argument to function "std::sprintf(char*, const char*, ...)".
2 Warning(s) detected.

下面是我添加的代码:

std::set<std::string> valid;
valid.insert(string("1-1"));

任何人都可以给我正确的方向吗?

以下是编译标志:

/export/SunStudio/SUNWspro/bin/CC -c -compat=5 -features=no%altspell -features=no%export -mt +d  -DTHREAD -DSOLARIS -staticlib=rwtools7 -library=rwtools7,iostream -DOWTOOLKIT_WARNING_DISABLED -Bsymbolic -PIC  -g0 -DBPDEBUG

但是对于示例程序,我只是编译如下:

/export/SunStudio/SUNWspro/bin/CC 0001.cc

2 个答案:

答案 0 :(得分:3)

听起来你在同一个函数中有两个名为“valid”的变量。将您的新变量重命名为valid2或将您的代码更改为仅使用名为valid的单个变量,并使用单个声明。

你不能只编写valid.insert(string("1-1"));之类的代码,除非它在函数内部。在下面的评论中,你说它不在函数中。您需要找到一些与std::set<std::string>具有相同效果的insert构造函数。

答案 1 :(得分:0)

最有可能的是,你在范围内有valid的另一个声明,因此多重声明错误。