无法将元素插入到 c++ 中的集合中
我写了一个示例程序。如下:
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
std::set<std::string> m;
m.insert("1-2-1-1");
return 0;
}
这完美运行,没有任何错误。
但我尝试创建一个集合并尝试在我的应用程序代码中将元素插入其中,这给了我一些错误。
/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
i have written a sample program.below:
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
std::set<std::string> m;
m.insert("1-2-1-1");
return 0;
}
This works perfectly without any errors.
But i try to create a set and try to insert elemts into it in my application code,it gives me the some errors.
/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"
below is the code that i have added.
but after commenting the below code.the compilation is successful
/*
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.
below is the code that i have added:
std::set<std::string> valid;
valid.insert(string("1-1"));
could any one give me the right direction?
Below is the compilation flags:
/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
but for the sample program i am simply compiling like below:
/export/SunStudio/SUNWspro/bin/CC 0001.cc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来好像同一个函数中有两个名为“valid”的变量。将新变量重命名为
valid2
或更改代码以仅使用名为valid
的单个变量和单个声明。您不能只编写像
valid.insert(string("1-1"));
这样的代码,除非它位于函数内部。在下面的评论中,您说它不在函数中。您需要找到与调用insert
具有相同效果的std::set
构造函数。It sounds like you have two variables in the same function that are named "valid". Rename your new variable to
valid2
or change your code to just use a single variable namedvalid
, with a single declaration.You can NOT just write code like
valid.insert(string("1-1"));
unless it is inside a function. In the comment below, you said it is not in a function. You need to find some constructor ofstd::set<std::string>
that has the same effect as callinginsert
.最有可能的是,您在范围内有另一个
valid
声明,因此会出现多重声明错误。Most likely, you have another declaration for
valid
in scope, hence the multiple declaration error.