Thread: Errors that I cannot figure out

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Question Errors that I cannot figure out

    I am getting a couple of errors when I compile this program and all I seem to be doing today is making them worse.
    This program is a homework program that I did before and it worked, but my program structure was totally different. I am re-writing it again as a class(we are working about classes , structs, inheiritance etc., and I wanted to try out this new material on a program that I had done before), but have screwed up in my functions and am just confused now. I know this is a case of messing with it for too long and the more I try to correct it the more I am messing it up.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class PString : public std::string
    {
    public:
    	PString( const std::string &aString );
    	bool isPalindrome() const;
    };
    
    PString::PString( const std::string &aString ) : std::string( aString )
    {
    
    }
    bool PString::isPalindrome() const
    {
    string reverse(string word)
    {
    string reverse;
    int length=word.length();
    for(int i=length;i>=0;i--)
    {
    reverse=reverse+word[i];
    }
    return reverse;
    }
    bool pal(string word)
    {
    bool pal=true;
    if(word!=reverse(word))
    {
    pal=false;
    }
    return pal;
    }
    	return true;
    }
    
    int main()
    {
    	std::string str;
    	std::cout << "This program is a palindrome-testing program. Enter a string to test:\n";
    	std::cin >> str;
    
    	// Create a PString object that will check strings
    	PString s(str);
    
    	// Check string and print output
    	if (s.isPalindrome())
    	{
    		std::cout << s << " is a palindrome";
    	}
    	else
    	{
    		std::cout << s << " is not a palindrome";
    	}
    	std::cout << std::endl;
    	system("pause");
    	return 0;
    }
    Specifically, I am getting error code C2601 on line string reverse(string word) and bool pal(string word).
    I get error C2780 two lines below that line and it tells me there is a problem in the void function- but I have no void function.
    What do I need to look at to get this to work? (Besides aspirin and a stiff drink!)

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    As a general rule, mentioning error codes is useless. It would be much more helpful to quote the exact error message (indicating, of course, which line of code it relates to whenever possible).

    In this case the problem is that you are defining a function -- isPalindrome -- and inside the definition of that function you are trying to define two more functions: reverse and pal.

    You can't define one function inside another, so define the functions reverse and pal as member functions of the class, and then if you want you can use them (call them) inside other functions.

    edit: Also, it's not a good idea to declare a variable named reverse inside the function which is also named reverse, or a variable named pal inside the function named pal. Give the variables names that distinguish them from the functions.
    Last edited by R.Stiltskin; 03-27-2011 at 11:24 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I don't think STL (container) classes are designed to be inherited from. PString could contain a std::string as a data member but should not be derived from one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Please look into indenting your code. It would certainly make it much easier to read. Like this:

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class PString : public std::string
    {
    public:
        PString( const std::string &aString );
        bool isPalindrome() const;
    };
    
    PString::PString( const std::string &aString ) : std::string( aString )
    {
    
    }
    bool PString::isPalindrome() const
    {
        string reverse(string word)
        {
            string reverse;
            int length=word.length();
            for(int i=length; i>=0; i--)
            {
                reverse=reverse+word[i];
            }
            return reverse;
        }
        bool pal(string word)
        {
            bool pal=true;
            if(word!=reverse(word))
            {
                pal=false;
            }
            return pal;
        }
        return true;
    }
    
    int main()
    {
        std::string str;
        std::cout << "This program is a palindrome-testing program. Enter a string to test:\n";
        std::cin >> str;
    
        // Create a PString object that will check strings
        PString s(str);
    
        // Check string and print output
        if (s.isPalindrome())
        {
            std::cout << s << " is a palindrome";
        }
        else
        {
            std::cout << s << " is not a palindrome";
        }
        std::cout << std::endl;
    
        system("pause");
    
        return 0;
    
    }
    You are defining your function reverse within another function, which is not allowed.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    C2601: 'function' : local function definitions are illegal

    in other words, nested functions are not allowed in C++. you can make 'reverse' and 'pal' private member functions then call them in isPalindrome.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hk_mp5kpdw View Post
    I don't think STL (container) classes are designed to be inherited from. PString could contain a std::string as a data member but should not be derived from one.
    The biggest problem of deriving from STL classes is that they tend to keep their members private, and not protected. It's not like it's bad, more like impracticable, I would say.
    Still, I am unsure if there are any good arguments against deriving from them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  3. Visual C++
    By gvector1 in forum Windows Programming
    Replies: 20
    Last Post: 03-14-2003, 11:37 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM