Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gcc 为什么std:string+;int给出编译错误?_Gcc_Stl_G++_Stdstring_Name Mangling - Fatal编程技术网

Gcc 为什么std:string+;int给出编译错误?

Gcc 为什么std:string+;int给出编译错误?,gcc,stl,g++,stdstring,name-mangling,Gcc,Stl,G++,Stdstring,Name Mangling,我们刚刚发现我们的同事认为他可以向std::string添加一个整数,并在代码中到处使用这种操作 我们没有看到这个操作的任何编译器错误,我也不明白为什么:没有操作符+(conststring&lhs,int-rhs)。int是否自动转换为char?(使用gcc Red Hat 4.1.2和-墙壁开关编译) 而且,最重要的是,我们如何找到int添加到std::string中的所有行?当您的同事只使用整数文本时,这些将转换为字符——但是,如果整数的值太大,您将看到警告。被调用的运算符是 std::s

我们刚刚发现我们的同事认为他可以向std::string添加一个整数,并在代码中到处使用这种操作

我们没有看到这个操作的任何编译器错误,我也不明白为什么:没有
操作符+(conststring&lhs,int-rhs)。int是否自动转换为char?(使用gcc Red Hat 4.1.2和-墙壁开关编译)


而且,最重要的是,我们如何找到int添加到std::string中的所有行?

当您的同事只使用整数文本时,这些将转换为
字符
——但是,如果整数的值太大,您将看到警告。被调用的运算符是

std::string operator+(const std::string &s, char c); // actually it's basic_string<> with some CharT...
但这并没有给我电话号码。。。至少呼叫站点在那里;)

< > >代码> -c/Cord>开关允许C++名称的解散。这也可以用binutls手动完成


有趣的是,对于
string
char
有一个
operator+
的定义,但只有在使用
operator+=
时,整数文本才会转换为
char
,我必须传递
char
文本(或值)才能使用
operator+


要查找操作员损坏的姓名

$ cat a.cpp
#include <string>
int main()
{
  std::string a = "moof ";
  a = a + char(1);
}
$ g++ a.cpp
$ objdump -t a.out | c++filt | grep operator+ | cut -d ' ' -f1
08048780
$ objdump -t a.out | grep 0804878
08048780  w    F .text  00000067              _ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_ERKS6_S3_
$cat a.cpp
#包括
int main()
{
std::string a=“moof”;
a=a+char(1);
}
$g++a.cpp
$objdump-t a.out | c++过滤器| grep运算符+| cut-d''-f1
08048780
$objdump-t a.out | grep 0804878
08048780 w F.text 00000067_zstplicist11char_traitssiceeesbit_T0_T1_ERKS6_S3_
最后一个是您正在搜索的名称-可用于grep w/o name demanling


我真的不知道有什么更好的方法来做这个…:/

C开关代表什么?我在CITOS ObjDip中没有它,它会把C++的ABI名字弄得模糊不清。没有它,
std::string::operator+=(char)
被命名为
\u znsplec
。实际上,我的objdump有这个开关。只是没有在帮助中列出。杰克,这是可能的。不要使用它并搜索损坏的操作员名称。编译一个测试程序,执行
str+'a'
,objdump-ta.out | c++filt,获取
operator+
objdump-t
的地址,不使用c++filt,并检查地址以获得损坏的名称。
$ cat a.cpp
#include <string>
int main()
{
  std::string a = "moof ";
  a = a + char(1);
}
$ g++ a.cpp
$ objdump -t a.out | c++filt | grep operator+ | cut -d ' ' -f1
08048780
$ objdump -t a.out | grep 0804878
08048780  w    F .text  00000067              _ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_ERKS6_S3_