S. Coughing
S. Coughing

Reputation: 186

Error while Pushing Elements from a std::string onto a Stack of String Type

I get this error while trying to loop through a string to push each element onto a stack of string type s1 in my isPalindrome() function.

no instance of overloaded function "std::stack<_Ty, _Container>::push [with _Ty=std::string, _Container=std::deque>]" matches the argument list

When I assign, the element that is at the top of the stack to a string variable an error pops up:

'std::stack>>::top': non-standard syntax; use '&' to create a pointer to member

Why does it mention std::deque?

#include <iostream>
#include <string>
#include <stack>
#include <algorithm> 

class Palindrome
{
public:
    Palindrome();
    void inputString();
    std::string convert2lower();
    bool isPalindrome();

private:
    std::string userstring;
    std::stack<std::string> s1;
};

Palindrome::Palindrome()
{}

void Palindrome::inputString()
{
    std::cout << "Enter a string: ";
    std::getline(std::cin,userstring);
}

std::string Palindrome::convert2lower()
{
    userstring.erase(remove(userstring.begin(), userstring.end(), ' '), userstring.end());
    userstring.erase(std::remove_if(userstring.begin(), userstring.end(), ispunct), userstring.end());
    transform(userstring.begin(), userstring.end(), userstring.begin(), tolower);
    return userstring;
}

bool Palindrome::isPalindrome()
{
    size_t n = userstring.size();

    for (size_t i = 0; i < n; ++i)
    {
        s1.push(userstring[i]);
    }

    std::string reversed;
    for (size_t i = 0; i < n; ++i)
    {
        std::string temp = s1.top;
        reversed.insert(i,temp);
        s1.pop();
    }

    if (reversed == userstring) 
    {
        return true;
    }

    return false;
}

int main()
{
    Palindrome p1;

    p1.inputString();

    std::cout << "\nCalling convert2lower(): " << std::endl;
    std::cout << "The new string is " << p1.convert2lower() << std::endl;
    std::cout << "\nCalling isPalindrome(): " << std::endl;

    if (!p1.isPalindrome())
    {
        std::cout << "String is NOT a Palindrome!" << std::endl;
    }
    else
    {
        std::cout << "String is a Palindrome!" << std::endl;
    }
}

Upvotes: 0

Views: 265

Answers (1)

AQuirky
AQuirky

Reputation: 5236

Here is your code fixed with the absolute minimum changes...

#include <iostream>
#include <string>
#include <stack>
#include <algorithm> 

class Palindrome
{
public:
    Palindrome();
    void inputString();
    std::string convert2lower();
    bool isPalindrome();

private:
    std::string userstring;
    std::stack<char> s1;
};

Palindrome::Palindrome()
{}

void Palindrome::inputString()
{
    std::cout << "Enter a string: ";
    std::getline(std::cin, userstring);
}

std::string Palindrome::convert2lower()
{
    userstring.erase(remove(userstring.begin(), userstring.end(), ' '), userstring.end());
    userstring.erase(std::remove_if(userstring.begin(), userstring.end(), ispunct), userstring.end());
    transform(userstring.begin(), userstring.end(), userstring.begin(), tolower);
    return userstring;
}

bool Palindrome::isPalindrome()
{
    size_t n = userstring.size();

    for (size_t i = 0; i < n; ++i)
    {
        s1.push(userstring[i]);
    }

    std::string reversed;
    for (size_t i = 0; i < n; ++i)
    {
        char temp = s1.top();
        reversed.insert(i, &temp, 1);
        s1.pop();
    }

    if (reversed == userstring)
    {
        return true;
    }

    return false;
}

int main()
{
    Palindrome p1;

    p1.inputString();

    std::cout << "\nCalling convert2lower(): " << std::endl;
    std::cout << "The new string is " << p1.convert2lower() << std::endl;
    std::cout << "\nCalling isPalindrome(): " << std::endl;

    if (!p1.isPalindrome())
    {
        std::cout << "String is NOT a Palindrome!" << std::endl;
    }
    else
    {
        std::cout << "String is a Palindrome!" << std::endl;
    }
}

Here are answers to your questions:

  1. no instance of...this is caused by using string instead of char as the stack type.
  2. non standard syntax...this is because you left the () off of pop.
  3. deque is mentioned because stack is a specialization of deque.

Upvotes: 2

Related Questions