I get an error when I try to compile my code. I can't figure it out. It says it's not declared but I think it is declared.

//
//
// This menu-driven program is used to allow the user to choose
// an application to be executed.
//
//
//
#include <string>
#include <cstdlib>
#include <iostream>
//
using namespace std;

//
// This class represents a string that can be tested to see if it is a palindrome

 class  PString :: public string
{
  public:

    //
    // THE FUNCTION PROTOTYPE FOR "isPalindrome"
    // THE FUNCTION RETURNS A BOOLEAN DATA VALUE
    // THE FUNCTION PARAMETER LIST IS EMPTY
  void bool isPalandrome();
    //
    //  MAKE "PString(string s)" A DERIVED CLASS
    // FROM THE BASE CLASS "string(s)"
 PString(string s)
  {
  }
};


// Determines whether this string is a  palindrome.
//
// DECLARE THE FUNCTION HEADER FOR "isPalindrome"
// THE FUNCTION RETURNS A BOOLEAN DATA VALUE
// THE FUNCTION PARAMETER LIST IS EMPTY
// USE A SCOPE RESOLUTION OPERATOR
class PString::isPalindrome(bool)
{
    int lower = 0;           // Start at beginning of string
    int upper = length()-1;  // Start at end of string
    //
    // IS A "while" LOOP THAT THE CONDITION
    // IS "lower" IS LESS THAN "upper"
    while (lower < upper)
    {
        if ((*this)[lower] != (*this)[upper])
        {
            // Found a mismatch
            return false;
        }
        //
        // INCREMENT "lower"

        lower++
        upper--;
    }
    // No mismatch found, so is palindrome
    return true;
}
//
//
// start main()
int main()
{

  //
  //  DECLARE A STRING VARIABLE NAMED "str"
  string str;
  char again =  ' ';

   //
    cout << "\n.\n\n";
  cout << "This program uses a class to test a string"
      << "\nto see if it is a palindrome.\n\n";
      //
      // Request input from user


// to test a strings to see if they are palindromes.
  do
  {
  cout << "Enter a string to test:  ";
  cin >> str;

  // Create a PString object that will check strings
  PString s(str);

  // Check string and print output
  if (s.isPalindrome())
      cout <<"\n" << s << " is a palindrome";
  else
      cout << "\n" << s << " is not a palindrome";
  cout << "\n\nDo you want to enter another string? (Y or N): ";
  cin >> again;
  cout << "\n\n"string str;
  }
  //
  // A "while" LOOP THAT THE CONDITION
  // IS DOES "again" EQUAL Y OR DOES "again" EQUAL y
   while(again=='y'||again=='Y');


     cout << "\n\n";
       //
//     system("pause");

}

835760a089d510b78e180baec60a3bf6

Recommended Answers

All 8 Replies

class PString :: public string ONE colon here for inheritance.

void bool isPalandrome(); Does it return void, or bool? Pick one, and also fix the spelling; later, you name this function isPalindrome.

lower++ Missing semi-colon

class PString::isPalindrome(bool) The first word here should be what kind of object this function returns. class has no place here. I suspect that you didn't mean to write bool where you have either.

cout << "\n\n"string str; And what exactly is this?

EDIT: Nevermind Moschops got there first! :)

//
//
// This menu-driven program is used to allow the user to choose
// an application to be executed.
//
//
//
#include <string>
#include <cstdlib>
#include <iostream>
//
using namespace std;

//
// This class represents a string that can be tested to see if it is a palindrome

 class  PString : public string
{
  public:

    //
    // THE FUNCTION PROTOTYPE FOR "isPalindrome"
    // THE FUNCTION RETURNS A BOOLEAN DATA VALUE
    // THE FUNCTION PARAMETER LIST IS EMPTY
   bool isPalindrome();
    //
    //  MAKE "PString(string s)" A DERIVED CLASS
    // FROM THE BASE CLASS "string(s)"
 PString(string s)
  {
  }
};


// Determines whether this string is a  palindrome.
//
// DECLARE THE FUNCTION HEADER FOR "isPalindrome"
// THE FUNCTION RETURNS A BOOLEAN DATA VALUE
// THE FUNCTION PARAMETER LIST IS EMPTY
// USE A SCOPE RESOLUTION OPERATOR
bool PString::isPalindrome()
{
    int lower = 0;           // Start at beginning of string
    int upper = length()-1;  // Start at end of string
    //
    // IS A "while" LOOP THAT THE CONDITION
    // IS "lower" IS LESS THAN "upper"
    while (lower < upper)
    {
        if ((*this)[lower] != (*this)[upper])
        {
            // Found a mismatch
            return false;
        }
        //
        // INCREMENT "lower"

        lower++;
        upper--;
    }
    // No mismatch found, so is palindrome
    return true;
}
//
//
// start main()
int main()
{

  //
  //  DECLARE A STRING VARIABLE NAMED "str"
  string str;
  char again =  ' ';

   //
    cout << "\n\n\n";
  cout << "This program uses a class to test a string"
      << "\nto see if it is a palindrome.\n\n";
      //
      // Request input from user


// to test a strings to see if they are palindromes.
  do
  {
  cout << "Enter a string to test:  ";
  cin >> str;

  // Create a PString object that will check strings
  PString s(str);

  // Check string and print output
  if (s.isPalindrome())
      cout <<"\n" << str << " is a palindrome";
  else
      cout << "\n" << str << " is not a palindrome";
  cout << "\n\nDo you want to enter another string? (Y or N): ";
  cin >> again;
  cout << "\n\n";
  }
  //
  // A "while" LOOP THAT THE CONDITION
  // IS DOES "again" EQUAL Y OR DOES "again" EQUAL y
   while(again=='y'||again=='Y');


     cout << "\n\n";
       //
//     system("pause");

}

The code runs but everything is a palindrome.

37e21a47982b69ce6634f4cf4b8230714007c48612ee8b343d7fee510b50600f

//
//
// This menu-driven program is used to allow the user to choose
// an application to be executed.
//
//
//
#include <string>
#include <cstdlib>
#include <iostream>
//
using namespace std;

//
// This class represents a string that can be tested to see if it is a palindrome

 class  PString : public string
{
  public:

    //
    // THE FUNCTION PROTOTYPE FOR "isPalindrome"
    // THE FUNCTION RETURNS A BOOLEAN DATA VALUE
    // THE FUNCTION PARAMETER LIST IS EMPTY
   bool isPalindrome();
    //
    //  MAKE "PString(string s)" A DERIVED CLASS
    // FROM THE BASE CLASS "string(s)"
 PString(string s)
  {
  }
};


// Determines whether this string is a  palindrome.
//
// DECLARE THE FUNCTION HEADER FOR "isPalindrome"
// THE FUNCTION RETURNS A BOOLEAN DATA VALUE
// THE FUNCTION PARAMETER LIST IS EMPTY
// USE A SCOPE RESOLUTION OPERATOR
bool PString::isPalindrome()
{
    int lower = 0;           // Start at beginning of string
    int upper = length()-1;  // Start at end of string
    //
    // IS A "while" LOOP THAT THE CONDITION
    // IS "lower" IS LESS THAN "upper"
    while (lower < upper)
    {
        if ((*this)[lower] != (*this)[upper])
        {
            // Found a mismatch
            return false;
        }
        //
        // INCREMENT "lower"

        lower++;
        upper--;
    }
    // No mismatch found, so is palindrome
    return true;
}
//
//
// start main()
int main()
{

  //
  //  DECLARE A STRING VARIABLE NAMED "str"
  string str;
  char again =  ' ';

   //
    cout << "\n\n\n";
  cout << "This program uses a class to test a string"
      << "\nto see if it is a palindrome.\n\n";
      //
      // Request input from user


// to test a strings to see if they are palindromes.
  do
  {
  cout << "Enter a string to test:  ";
  cin >> str;

  // Create a PString object that will check strings
  PString s(str);

  // Check string and print output
  if (s.isPalindrome())
      cout <<"\n" << str << " is a palindrome";
  else
      cout << "\n" << str << " is not a palindrome";
  cout << "\n\nDo you want to enter another string? (Y or N): ";
  cin >> again;
  cout << "\n\n";
  }
  //
  // A "while" LOOP THAT THE CONDITION
  // IS DOES "again" EQUAL Y OR DOES "again" EQUAL y
   while(again=='y'||again=='Y');


     cout << "\n\n";
       //
//     system("pause");

}

The code runs but everything is a palindrome.

Your PString object is empty. It has no letters in it. When you create your PString, you need to put some letters in it.

What would be an example that I could use?

Your constructor for your PString has access to the letters it needs; you just need to populate the PString.

Because the PString is a kind of std::string, you can use the std::string push_back() function, and you can read the letters from the parameter s.

Here's a constructor for your PString object that does this; should make it clear what's going on.

 PString(string s)
  {
 for (int i=0; i< s.size(); ++i)
    {
      this->push_back(s[i]);
    }
  }
//
//
// This menu-driven program is used to allow the user to choose
// an application to be executed.
//
//
//
#include <string>
#include <cstdlib>
#include <iostream>
//
using namespace std;

//
// This class represents a string that can be tested to see if it is a palindrome

 class  PString : public string
{
  public:

    //
    // THE FUNCTION PROTOTYPE FOR "isPalindrome"
    // THE FUNCTION RETURNS A BOOLEAN DATA VALUE
    // THE FUNCTION PARAMETER LIST IS EMPTY
   bool isPalindrome();
    //
    //  MAKE "PString(string s)" A DERIVED CLASS
    // FROM THE BASE CLASS "string(s)"
  PString(string s)
  {
 for (int i=0; i< s.size(); ++i)
    {
      this->push_back(s[i]);
    }
  }

};


// Determines whether this string is a  palindrome.
//
// DECLARE THE FUNCTION HEADER FOR "isPalindrome"
// THE FUNCTION RETURNS A BOOLEAN DATA VALUE
// THE FUNCTION PARAMETER LIST IS EMPTY
// USE A SCOPE RESOLUTION OPERATOR
bool PString::isPalindrome()
{
    int lower = 0;           // Start at beginning of string
    int upper = length()-1;  // Start at end of string
    //
    // IS A "while" LOOP THAT THE CONDITION
    // IS "lower" IS LESS THAN "upper"
    while (lower < upper)
    {
        if ((*this)[lower] != (*this)[upper])
        {
            // Found a mismatch
            return false;
        }
        //
        // INCREMENT "lower"

        lower++;
        upper--;
    }
    // No mismatch found, so is palindrome
    return true;
}
//
//
// start main()
int main()
{

  //
  //  DECLARE A STRING VARIABLE NAMED "str"
  string str;
  char again =  ' ';

   //
    cout << "\n\n\n";
  cout << "This program uses a class to test a string"
      << "\nto see if it is a palindrome.\n\n";
      //
      // Request input from user


// to test a strings to see if they are palindromes.
  do
  {
  cout << "Enter a string to test:  ";
  cin >> str;

  // Create a PString object that will check strings
  PString s(str);

  // Check string and print output
  if (s.isPalindrome())
      cout <<"\n" << str << " is a palindrome";
  else
      cout << "\n" << str << " is not a palindrome";
  cout << "\n\nDo you want to enter another string? (Y or N): ";
  cin >> again;
  cout << "\n\n";//string str;
  }
  //
  // A "while" LOOP THAT THE CONDITION
  // IS DOES "again" EQUAL Y OR DOES "again" EQUAL y
   while(again=='y'||again=='Y');


     cout << "\n\n";
       //
//     system("pause");

}

I tried out the example you gave me. The code worked. Thank you so much Moschops

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.