简体   繁体   English

我运行程序后,C ++给我basic_string :: _ S_construct null无效错误

[英]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 valid what():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.

相关问题 C ++:脚本中发生异常:basic_string :: _ S_construct NULL无效 - C++ : Exception occurred in script: basic_string::_S_construct NULL not valid basic_string :: _ S_construct null无效 - basic_string::_S_construct null not valid openCV basic_string :: _ S_construct null无效 - openCV basic_string::_S_construct null not valid 野牛:what():basic_string :: _ S_construct null无效 - Bison: what(): basic_string::_S_construct null not valid std :: string(empty char *)错误:basic_string :: _ S_construct null无效错误 - std::string(empty char*) error: basic_string::_S_construct null not valid error 在函数参数/套接字中发送std :: string时basic_string :: _ S_construct null无效错误 - basic_string::_S_construct null not valid error while sending std::string in function argument/socket 抛出&#39;std :: logic_error&#39;what()实例后调用终止终止:basic_string :: __ S_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 字符串 std::logic_error: basic_string::_S_construct null 无效 - String std::logic_error: basic_string::_S_construct null not valid std :: logic_error:basic_string :: _ s_construct null无效 - std::logic_error: basic_string::_s_construct null not valid 如何避免错误:在抛出&#39;std :: logic_error&#39;的实例后调用terminate what():basic_string :: _ S_construct null无效 - How to avoid the error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM