Error in printing all substrings of a string - c++

This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?

As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());

you using string, in function is char try to use char[] s;

use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());

You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}

Related

Trouble with pointers and functions in C++

I have a function like this:
void getSprite(string *spriteLines[SPRITE_YSIZE]);
And then I have a calling to the function like this:
int main() {
string *spriteLines[SPRITE_YSIZE];
getSprite(spriteLines);
By here, everything OK. But I decided to declare spriteLines as a string instead of a pointer so I changed the code to this:
int main() {
string spriteLines[SPRITE_YSIZE];
getSprite(&spriteLines);
And an error shows up:
error: cannot convert ‘std::__cxx11::string (*)[5] {aka std::__cxx11::basic_string<char> (*)[5]}’ to ‘std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}’ for argument ‘1’ to ‘void getSprite(std::__cxx11::string**)’ getSprite(&spriteLines);
Do someone of you knows why? I can't understand this.
Extra data: I'm using Eclipse Oxygen v1 and GNU G++.
In the first example, you declared an array of string pointers. In the second example you declared an array of strings. Both arrays are passed your function as a pointer. Your error occurs because the are different "types".
In the second example you would need to change your getSprite function to:
void getSprite(string spriteLines[SPRITE_YSIZE])

VS error C2664 (return string from a function) C++

I'm sure this was answered somewhere on the site but can not find it... I'm writing under VS10 in C++. I'm writing a class that holds details of a student. One of the members is
string studentName[30];
There should be a function that returns this string on request, this could be done using traditional C strings and a pointer however I would like to use C++ strings.
My get function looks like so:
string Student::getName()
{
return studentName;
}
On compile, I get this error from VS10:
Error 1 error C2664:
'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const
std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1
from 'std::string [30]' to 'const std::basic_string<_Elem,_Traits,_Ax>
&' f:\c++\hw1\hw1\hw3\hw3.cpp 56 1 HW3
I'm not sure what this means. If anyone can clarify I would be thankful. Also, in these get functions is it common to return a reference for the string or actual literal values (hope this is the correct lingo).
StudentName declared as such:
protected:
string studentName[30];
int studentGrades[8];
int studentAge;
};
You defined data member studentName as having type string[30]
string studentName[30];
At the same time function getName has return type string
string Student::getName()
Now please answer how has the compiler to convert an object of type string[30] to an object of type string?
I think you meant the following
string Student::getName()
{
return studentName;
}
protected:
string studentName;
int studentGrades[8];
int studentAge;
};
that is instead of string studentName[30] there should be simply string studentName because I do not see a greate sense to store the name of a student in 30 strings though maybe in Brasil there are names that contains 30 words.:)
studentName is a string* (array -> pointer) but you return a string from the function. The error message says it all. Either return a single string or
string* Student::getName()
{
return studentName;
}

Error: invalid conversion from 'char' to 'const char*'

I want to make function that can return two new string that is the composition of the old one but I got an above error.
string constru(string num, int pos_be, int pos_end)
{
string f_num="";
string s_num="";
f_num.append(num.at(pos_be));
f_num.append(num.at(pos_end));
num.erase(pos_be);
num.erase(pos_end);
for(int i=0; i<num.size();i++)
{
s_num.append(num.at(i));
}
return f_num,s_num;
}
The Error is at the line f_num.append(num.at(pos_be)) as well as the other lines that I used append with string. Does anyone want know what went wrong here?
The problem here is, the at function returns a char and not a string. But the append function supports a string. So you get this error. Convert the char to string before you append.
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));

Error request for member 'find'

string input;
string code[4];
if (code.find(o) == input.find(o))
{
}
For this line it gives me the error: request for member 'find' in 'code', which is of non-class type 'std::string [4]'
Both string input and string code have string values in them.
The compiler is telling you that code is an array of strings, so you need something like
code[someIndex].find(o) == ....
The error says all , code is an array of std::string
compare using
code[i].find(o)
i = looping index
Just read the error:
error: request for member 'find' in 'code', which is of non-class type 'std::string [4]
It is telling you that:
code is of type std::string [4] (which is an array of 4 std::string objects)
it couldn't find the member function find you are requesting
Just select the correct index for the string you want to call find on or do a loop:
for (int i = 0; i < 4; i++)
if (code[i].find(o) == input.find(o))
// ...
Note that, if you can, you should try to avoid C-style arrays and use std::array (since C++11) or std::vector instead.

atoi and string array

I have a string array and an integer array. I want to convert the elements of string array to integer and then store them in the integer array. I wrote this code :
string yuzy[360];
int yuza[360];
for(int x = 0;x<360;x++)
{
if(yuzy[x].empty() == false)
{
yuza[x]=atoi(yuzy[x]);
cout<<yuza[x]<<endl;
}
else
continue;
}
this piece of code gives this error:
error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
When I write the content of the string (-75dbm) in atoi function it works fine. But when I write (yuzy[x]), I get the error. How can I make atoi works well with string array?
Thanks.
atoi() takes C strings (char pointers) and not C++ string objects. Use
atoi(yuzy[x].c_str());
instead.
As an alternative to atoi, you could use std::stoi and related functions, if you have C++11 support.
yuza[x] = std::stoi(yuzy[x]);
atoi accept a c-style string as parametter, so, you could use atoi(yuzy[x].c_str());