[英]C++ giving me basic_string::_S_construct null not valid error after I run my program
I am a newbie to C++ and I've tried to write a simple string reverse program. 我是C ++的新手,我试图编写一个简单的字符串反向程序。 When I compile it, everything is OK, but when I run it, I get the following error:
当我编译它时,一切都很好,但是当我运行它时,我收到以下错误:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted (core dumped)
What I am doing wrong? 我做错了什么? Here is my code.
这是我的代码。
#include <iostream>
using namespace std;
string reverse_string(char* argv[], int i);
int main(int argc, char* argv[])
{
for (int i = 0; i < argc; i++)
{
cout << reverse_string(argv, i) << endl;
}
return 0;
}
string reverse_string(char* argv[], int i)
{
string arg = argv[i + 1];
string output;
int length = arg.length();
for (int index = 1; index <= length; index++)
{
output += arg[length-index];
}
return output;
}
This: argv[i + 1]
in the construction of your reversed string should be argv[i]
and the main loop should be for (i=1; i<argc; ++i)
这:反向字符串的构造中的
argv[i + 1]
应为argv[i]
,主循环应for (i=1; i<argc; ++i)
And there are simpler ways to reverse a string: 还有更简单的方法来反转字符串:
std::string reverse_string(char* argv[], int i)
{
std::string arg = argv[i];
return std::string(arg.rbegin(), arg.rend());
}
This error message 此错误消息
terminate called after throwing an instance of 'std::logic_error'
在抛出'std :: logic_error'的实例后终止调用
what(): basic_string::_S_construct null not validwhat():basic_string :: _ S_construct null无效
means that you are trying to call constructor of std::string passing as argument pointer NULL. 表示您正在尝试调用std :: string的构造函数作为参数指针NULL传递。 The problem is that
*(argv + argc)
is NULL pointer. 问题是
*(argv + argc)
是NULL指针。
Also take into account that you must include header <string>
还要考虑到必须包含标题
<string>
As for the reverse function then it can be written much simpler then that of you. 至于反向功能,它可以写得比你简单得多。 First of all it could have only one parameter of type
const char *
首先,它只能有一个
const char *
类型的参数const char *
For example 例如
#include <iostream>
#include <string>
std::string reverse_string( const char* s );
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
std::cout << argv[i] << " -> " << reverse_string( argv[i] ) << std::endl;
}
return 0;
}
std::string reverse_string( const char* s )
{
std::string arg( s );
return std::string( arg.rbegin(), arg.rend() );
}
Ok thanks for help everyone! 好的,谢谢大家的帮助! I just didn't want to reverse argument 0, which is program's name.
我只是不想反转参数0,它是程序的名称。 But I found another way around it.
但是我发现了另一种解决方法。 If anyone is interested, here is the fixed code:
如果有人感兴趣,这里是固定代码:
#include <iostream>
using namespace std;
string reverse_string(char* argv[], int i);
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
cout << argv[i] << " -> " << reverse_string(argv, i) << endl;
}
return 0;
}
string reverse_string(char* argv[], int i)
{
string arg = argv[i];
string output;
int length = arg.length();
for (int index = 1; index <= length; index++)
{
output += arg[length-index];
}
return output;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.