CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    3

    Beginner Programmer Help!

    Hello everybody,

    I'm new to programming. I've been trying to teach myself C++, and so far everything seems to be working out very well. However, I've recently hit a snag while trying to compile the following code. I'm using XCode on a Mac as my compiler. Here's the code:

    Header File "header.h":

    #include <iostream.h>
    class Cat
    {
    public:
    Cat (int initialAge);
    ~Cat();
    int GetAge() {return itsAge;}
    void SetAge (int age) {itsAge = age;}
    void Meow() {cout << "Meow.\n";}
    private:
    int itsAge;
    };



    Main program file "main.cpp":

    //Demonstrates inline functions and inclusions of header files

    //inclusion of header file
    //This should include iostream.h and declare a class Cat
    #include "header.h"

    Cat::Cat(int initialAge)
    {
    itsAge = initialAge;
    }

    Cat::~Cat()
    {
    }

    //Create a cat, set its age, have it meow, tell us its age, and then meow again
    int main()
    {
    Cat.Frisky(5);
    Frisky.Meow();
    cout << "Frisky is a cat who is " << Frisky.GetAge() << " years old.\n";
    Frisky.Meow();
    Frisky.SetAge(7);
    cout << "Now Frisky is ";
    cout << Frisky.GetAge() << " years old.\n";
    return 0;
    }



    The compiler returns two errors: one for the Cat.Frisky(5); line, and one for the Frisky.Meow(); line. The errors are:

    error: expected unqualified-id before '.' token

    and

    error: 'Frisky' was not declared in this scope



    I'm honestly not ever sure if this forum is where I should be posting this. If I have the wrong place, I would appreciate somebody pointing me in the right direction.

    Thanks,

    helixed

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Beginner Programmer Help!

    First, you should have used code tags. Code without proper indenting is hard to read. However:

    Quote Originally Posted by helixed
    int main()
    {
    Cat.Frisky(5);
    Frisky.Meow();
    I assume you've declared variables before, such as ints. Which of the following would you have written?
    Code:
    int.i(0);
    int i = 0;
    It's the same for classes.

  3. #3
    Join Date
    Jun 2008
    Posts
    3

    Re: Beginner Programmer Help!

    That's for the quick reply. I'll use code tags in the future.

    I'm still not quite clear what you mean by this. I've been trying to use this guide to learn C++. The code I pasted was copied line for line in the guide from the Day Six entry, with the exception of the file names being changed. Could you clarify what you mean, or if my guide isn't up to par, could you suggest a different one?

    Thanks,

    helixed

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Beginner Programmer Help!

    Quote Originally Posted by helixed
    That's for the quick reply. I'll use code tags in the future.

    I'm still not quite clear what you mean by this. I've been trying to use this guide to learn C++. The code I pasted was copied line for line in the guide from the Day Six entry, with the exception of the file names being changed. Could you clarify what you mean, or if my guide isn't up to par, could you suggest a different one?

    Thanks,

    helixed
    He means the syntax for creating an object like that is

    Cat Frisky(5);
    not
    Cat.Frisky(5);

  5. #5
    Join Date
    Jun 2008
    Posts
    3

    Re: Beginner Programmer Help!

    Oh, okay, now I get it. I feel pretty stupid at this point.

    Thanks for the help,

    helixed

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Beginner Programmer Help!

    Quote Originally Posted by helixed
    Oh, okay, now I get it. I feel pretty stupid at this point.
    Don't worry. As you become more experianced with C++ you'll find that your 'stupid mistakes' become larger and much more impressive.

  7. #7
    Join Date
    Jun 2005
    Posts
    315

    Re: Beginner Programmer Help!

    ...and expensive.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Beginner Programmer Help!

    Quote Originally Posted by helixed
    Hello everybody,

    I'm new to programming. I've been trying to teach myself C++, and so far everything seems to be working out very well.
    Whatever youre using to teach yourself, change it, and quick.
    Code:
    #include <iostream.h>
    This is not the correct header. The correct standard header is <iostream>, not <iostream.h>

    If you're using a book that has <iostream.h>, then the book is either old, wrong. C++ has been standardized for at least a decade now, and headers such as <iostream.h> are not to be used, and in some cases, do not even exist for some compilers.

    Since you're using <iostream.h>, you've also more than likely missed what a namespace is and how to use it. This is covered in chapter 1 of any good, modern C++ book.

    The correct "hello world" C++ program is written this way:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
    }
    If you cannot compile this code, then get yourself another compiler, as the compiler (as well as whatever material you're using to learn C++) is outdated and shouldn't be used.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2008
    Location
    *****, Nigeria
    Posts
    40

    Re: Beginner Programmer Help!

    I agree, are you sure your using a study guide or some tutor online?? okay I recommend you get C++ for Dummies - 5th edition;

    I'm a beginner also and has really worked for me.


    Regards,
    Richard Aberefa.

    <edited by admin>
    Last edited by Brad Jones; July 1st, 2008 at 07:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured