Reputation: 1920
I have this function:
std::string reverse_words(const std::string& in)
{
std::stringstream ss(in);
std::string item, result;
while (std::getline(ss, item, ' ')) {
the_stack.push(item);
}
for (int i = 0; i < the_stack.size(); i++)
{
result += " " + the_stack.top();
the_stack.pop();
}
return result;
}
However, when I try to call it here:
int main(int argc, char** argv) {
reverse* s = new reverse();
std::string in = "fdgdfgbjfd gdf gjhfd";
std::string reverse_words = s.reverse_words(&in);
cout << reverse_words;
return 0;
}
I get an error on std::string reverse_words = s.reverse_words(&in);
(request for member 'reverse_words' in 's', which is of pointer type 'reverse*' (maybe you meant to use '->' ?)). I am unable to understand where I am going wrong in this.
Upvotes: 0
Views: 50
Reputation: 56547
Your function
std::string reverse_words(const std::string& in)
takes a const reference, but you pass an address. Remove the &
when passing the argument
std::string reverse_words = s.reverse_words(&in);
^change to-> ^ remove this
Next, s
is a pointer, so you have to call the member function via the ->
operator, like
s->reverse_words(in);
Side note: even if internally references are usually implemented as pointers, you should not use pointer syntax when passing an argument by reference. In other words, the &
address-of operator should never be present when passing by reference.
Upvotes: 3