错误C3867'std :: basic_string ,std :: allocator > :: c_str':非标准语法;使用'&'创建指向成员的指针

时间:2022-12-01 09:43:18

I have this piece of code, I don't understand why it is displaying this error?

我有这段代码,我不明白它为什么显示这个错误?

string messege = "aaa";
char tmp[50];
strcpy_s(tmp, messege.length(), messege.c_str);
char* s = NULL;
s = &(tmp[0]);

Can someone help?

有人可以帮忙吗?

2 个解决方案

#1


0  

In this line, you've forgotten the parentheses after the call to c_str:

在这一行中,您在调用c_str后忘记了括号:

strcpy_s(tmp, messege.length(), messege.c_str);

Adding the missing parentheses should fix this.

添加缺少的括号应该解决这个问题。

That said, it's unusual to want to mix C-style and C++-style strings this way. You may want to think about whether what you're doing is appropriate.

也就是说,想要以这种方式混合C风格和C ++风格的字符串是不寻常的。您可能想要考虑您所做的事情是否合适。

#2


0  

c_str is a member function of std::string, so you need to call it with ()

c_str是std :: string的成员函数,所以你需要用()调用它

strcpy_s(tmp, messege.length(), messege.c_str());

This will solve your problem.

这将解决您的问题。

#1


0  

In this line, you've forgotten the parentheses after the call to c_str:

在这一行中,您在调用c_str后忘记了括号:

strcpy_s(tmp, messege.length(), messege.c_str);

Adding the missing parentheses should fix this.

添加缺少的括号应该解决这个问题。

That said, it's unusual to want to mix C-style and C++-style strings this way. You may want to think about whether what you're doing is appropriate.

也就是说,想要以这种方式混合C风格和C ++风格的字符串是不寻常的。您可能想要考虑您所做的事情是否合适。

#2


0  

c_str is a member function of std::string, so you need to call it with ()

c_str是std :: string的成员函数,所以你需要用()调用它

strcpy_s(tmp, messege.length(), messege.c_str());

This will solve your problem.

这将解决您的问题。