I'm trying to debug this code but the GDB error is so long I'm having a hard time to decipher it, Vim doesn't seem to want to compile it with GCC and compiler checker specified to compile for c++ 11 standard although c++ shell seems to compile it just fine.
Error : Program received signal SIGSEV, Segmentation Fault.
0x00007ffff7b769bb in std::__cxxll::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxxll:basic_string<char, std::char_traits<char>, std::allocator<char> > const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#include <iostream>
#include <string>
#include <vector>
std::string mirror (std::string word)
{
if (word.length() <= 0 )
{
return {};
}
else
{
char lastLetter = word.back(); // Get last index of the string.
word.pop_back(); // Remove last index of the string
return lastLetter + mirror (word); // Put last letter on the front of the word and pass the new word without the last letter to the next function.
}
}
std::vector<std::string> splitString( std::vector<std::string> split, std::string to_split )
{
std::size_t position = 0, space;
while((space = to_split.find_first_of(' ',position)) != std::string::npos)
{
split.push_back(to_split.substr(position, space - position));
position = space + 1;
}
split.push_back(to_split.substr(position));
return split;
}
void outputString ( std::string to_split )
{
std::vector<std::string> splitWords = splitString( splitWords, to_split );
for ( int i=0; i<splitWords.size(); i++)
{
std::string output = mirror(splitWords[i]);
std::cout << output << " ";
}
}
int main()
{
std::string test = "one two three four";
outputString ( test );
return 0;
}
Yes, I have fixed that but the error remains, I had a similar issue recently and the cause was accessing a vector without any elements but I'm pushing back and returning the vector so that shouldn't be an issue.
Line 34:
std::vector<std::string> splitWords = splitString( splitWords, to_split );
How does this work? You appear to have splitWords on the RHS. You don't need it as an argument to the function: you need to declare a vector to return the result in the function splitString(...) itself.
Line 34:
std::vector<std::string> splitWords = splitString( splitWords, to_split );
How does this work? You appear to have splitWords on the RHS. You don't need it as an argument to the function: you need to declare it in the function splitString(...) itself.
This has been the bug, thank you for your help and for my inability to update the code for You. I was contemplating on what is going wrong and forgot to update the source-code, so it didn't reflect the fact that I have changed the return type of the function to void instead of std::string on the outputString.
I have removed the need to pass the vector as an argument in the splitString function and just declared a local vector within the function.
#include <iostream>
#include <string>
#include <vector>
std::string mirror (std::string word)
{
if (word.length() <= 0 )
{
return {};
}
else
{
char lastLetter = word.back(); // Get last index of the string.
word.pop_back(); // Remove last index of the string
return lastLetter + mirror (word); // Put last letter on the front of the word and pass the new word without the last letter to the next function.
}
}
std::vector<std::string> splitString( std::string to_split ) //<=======
{
std::vector<std::string> split; //<=======
std::size_t position = 0, space;
while((space = to_split.find_first_of(' ',position)) != std::string::npos)
{
split.push_back(to_split.substr(position, space - position));
position = space + 1;
}
split.push_back(to_split.substr(position));
return split;
}
void outputString ( std::string to_split )
{
std::vector<std::string> splitWords = splitString( to_split ); //<=======
for ( int i=0; i<splitWords.size(); i++)
{
std::string output = mirror(splitWords[i]);
std::cout << output << " ";
}
}
int main()
{
std::string test = "one two three four";
outputString ( test );
}
01-191944.cxx:29:53: warning: variable 'splitWords' is uninitialized when used
within its own initialization [-Wuninitialized]
std::vector<std::string> splitWords = splitString(splitWords, to_split);
But G++ does not warn here.
You may find it useful to install at least one other compiler: often, there is an appreciable difference between the quality of any particular diagnostic. Also, consider making sure to turn on all useful warnings, using flags like -Wall -Wextra -pedantic-errors.