-2

cpp

#include <iostream>
#include <stack>
#include <string>
using namespace std;

class Pstring : public string
{
public:
    Pstring(string revWrd);
    bool isPalindrome(string ip);
};

bool isPalindrome(string revWrd)
{
    int test;
    int left = 0;
    int right = revWrd.size()-1;
    int length = left;

    while (test != length)
    {
        test++;

        if ((revWrd[left] != revWrd[right]))
            return false;
        else
            return true;
        left++;
        --right;
    }
}

int main()
{
    string word;
    bool palindrome;
    Pstring palindromeTested(word);

    cout << "Enter a word or sentence to have it\n";
    cout << "reversed and tested if it is a palindrome: ";
    getline(cin, word);
    palindrome = palindromeTested.isPalindrome(word);
    if(palindrome==true)
        cout<<"This is a palindrome"<<endl;
    else
        cout <<"That was not a palindrome";
    return 0;
}

i get 2 error codes which are 1 "undefined reference to `Pstring::Pstring(std::__cxx11::basic_string, std::allocator >)'|" and 2 "undefined reference to `Pstring::isPalindrome(std::__cxx11::basic_string, std::allocator >)'|
Some Cruz
  • 1
  • 1

1 Answers1

2

The error is because the member functions in the class Pstring are not defined.

The class doesn't seem used effectively, so you should remove that.

Also there are some other errors:

  • The variable test is used without being initialzed.
  • return true; is in wrong place, making strings like abcda be judged as palindrome.
#include <iostream>
#include <stack>
#include <string>
using namespace std;

bool isPalindrome(string revWrd)
{
    int test = 0; // initialize test
    int left = 0;
    int right = revWrd.size()-1;
    int length = left;

    while (test != length)
    {
        test++;

        if ((revWrd[left] != revWrd[right]))
            return false;
        // else
            // don't return here to check all parts of the string
            
        left++;
        --right;
    }
    return true; // return at end of funciton
}

int main()
{
    string word;
    bool palindrome;

    cout << "Enter a word or sentence to have it\n";
    cout << "reversed and tested if it is a palindrome: ";
    getline(cin, word);
    palindrome = isPalindrome(word);
    if(palindrome==true)
        cout<<"This is a palindrome"<<endl;
    else
        cout <<"That was not a palindrome";
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70