Thread: Problem with semicolon.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Problem with semicolon.

    I am working from the book SAMS teach yourself C++ and I am encountering the following problem while trying to use codeblocks to build the following code.

    I presume the error on line 22 in main.cpp has to do with the errors in include\Cat.h but the other one one line 25 I have no idea about what is causing it as well as the 2 errors in include\Cat.h.

    Hoped someone here could help me out.

    main.cpp
    Code:
    // Demonstration of inline functions and inclusion of header files.
    // Be sure to include the header files!
    #include "include\Cat.h"
    
    Cat::Cat(int initialAge)        // Constructor
    {
        itsAge = initialAge;
    }
    
    Cat::~Cat()                     // Deconstructor
    {
    
    }
    
    // Create a cat, set its age, have it meow, tell us its age, then make it meow again
    int main()
    {
        Cat Frisky(5);
        Frisky.Meow();
    
        std::cout << "Frisky is a cat who is ";
        std::cout << GetAge() << " years old.\n";
    
        Frisky.Meow();
        Frisky SetAge (7);
    
        std::cout << "Now Frisky is ";
        std::cout << GetAge() << " years old.";
    
        return 0;
    }
    Errors
    Line 22, 'GetAge' was not declared in this scope
    Line 25, expected ';' before 'SetAge'

    include\Cat.h
    Code:
    #include<iostream>
    
    class Cat
    {
        public:
            Cat(int initialAge);
            ~Cat();
            int GetAge const { return itsAge;}               // Inline!
            void SetAge(int age) { itsAge = age;}            // Inline!
            void Meow() const { std::cout << "Meow!\n";}     // Inline!
        private:
            int itsAge;
    };
    Errors
    Line 8, expected ';' before 'const'
    Line 9, expected ';' before 'void'

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Code:
            int GetAge const { return itsAge;}               // Inline!
    This function is missing it's parentheses.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Also, the book you are using is very poor. You should be looking at either Accelerated C++ or PPP by Bjarne Stroustrup. They are widely regarded as the better books to learn from.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by Ushakal View Post
    This function is missing it's parentheses.
    I presume you mean I need to re-write it as:
    Code:
    int GetAge const { return (itsAge);}               // Inline!
    However that does not change a thing the same errors keep coming while trying to build.
    Quote Originally Posted by darren78 View Post
    Also, the book you are using is very poor. You should be looking at either Accelerated C++ or PPP by Bjarne Stroustrup. They are widely regarded as the better books to learn from.
    I could not find any other books to learn from. Unless I wanted to wait for 1 month for them to be delivered.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Kitt3n View Post

    I could not find any other books to learn from. Unless I wanted to wait for 1 month for them to be delivered.
    Where do you live? Some small remote African village?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Kitt3n View Post
    I presume you mean I need to re-write it as:
    Code:
    int GetAge const { return (itsAge);}               // Inline!
    However that does not change a thing the same errors keep coming while trying to build.
    No, you do not need brackets when returning a value.

    Look at the following function declarations:

    Code:
    int main();
    void Print(const std::string& str);
    void GetInput();
    All functions must include parentheses, whether or not they have any parameters, as part of their signature. Do you see what the problem is now?
    Last edited by Ushakal; 07-16-2010 at 07:40 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by darren78 View Post
    Where do you live? Some small remote African village?
    It is not my fault Americans are that slow sending packages to Europe.
    Quote Originally Posted by Ushakal View Post
    No, you do not need brackets when returning a value.
    All functions must include parentheses, whether or not they have any parameters, as part of their signature. Do you see what the problem is now?
    I almost wanted to say yes but editing the code like this still results in the same errors.
    Code:
    int GetAge(const) { return itsAge;}
    I thought you meant that I was declaring a function without using parentheses behind it.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Kitt3n View Post
    Code:
    int GetAge(const) { return itsAge;}
    I thought you meant that I was declaring a function without using parentheses behind it.
    The const keyword should not be within the parentheses.

    You got it right with this function:

    Code:
    void Meow() const { std::cout << "Meow!\n";}     // Inline!
    If you write the GetAge function's signature in the same way, it should be fine.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    62
    I just made that change but the result is still the same with the errors.
    Code:
    int GetAge() const { return itsAge;}

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Kitt3n View Post
    I just made that change but the result is still the same with the errors.
    Code:
    int GetAge() const { return itsAge;}
    That particular error should be fixed, but you have more in your main function. I overlooked this before, sorry:

    Code:
        std::cout << GetAge() << " years old.\n";
    
    ...
    
        Frisky SetAge (7);
    
    ...
    
        std::cout << GetAge() << " years old.";
    SetAge and GetAge are both member functions of the Frisky object. As such, you need to use the dot operator to access the member functions of this particular object's instance.

    Code:
    Frisky.SetAge(7);
    Frisky.GetAge();

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    62
    I overlooked those as well, stupidity I guess. But even with all those changes made codeblocks still gives me the exact same errors as given in the first posting they did not seem to have changed anything.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Hmm, I can compile your code after fixing those errors... Can you post what you have now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM