explaining what exceptions::what is and my errors.

hi there guys. i'm having problems understanding this error!

1
2
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid


Does this mean i have to throw an exception like this tutorial?
http://www.cplusplus.com/reference/exception/exception/what/

should i post my code here because my professor told me i can't post anything on this forum and he checks!
You should inspect the call stack and see where in your code you are performing the operation that ends up leading to the exception. That specific error, as I recall, is likely from passing a char* that is null to an std::string. Please confirm you aren't doing that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

int main()
{
    std::string stra ; // fine, default constructed
    std::cout << "1\n" << std::flush ;
    
    std::string strb = "hello" ; // fine, constructed with const char*
    std::cout << "2\n" << std::flush ;

    std::string str(nullptr) ; // *** error: null pointer 
    std::cout << "3\n" << std::flush ;
}

g++ -std=c++11 -O2 -Wall -Wextra -pedantic-errors -g -O0 main.cpp && ./a.out
1
2
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
bash: line 7:  9199 Aborted                 (core dumped) ./a.out

http://coliru.stacked-crooked.com/a/40721b599d510f35


> my professor told me i can't post anything on this forum and he checks!

You professor is being asinine, and I hope he reads this when he checks.
Last edited on
Topic archived. No new replies allowed.