|
-
June 29th, 2008, 05:35 PM
#1
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
-
June 29th, 2008, 05:39 PM
#2
Re: Beginner Programmer Help!
First, you should have used code tags. Code without proper indenting is hard to read. However:
 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.
-
June 29th, 2008, 06:03 PM
#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
-
June 29th, 2008, 06:20 PM
#4
Re: Beginner Programmer Help!
 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);
-
June 29th, 2008, 06:23 PM
#5
Re: Beginner Programmer Help!
Oh, okay, now I get it. I feel pretty stupid at this point.
Thanks for the help,
helixed
-
June 30th, 2008, 04:54 AM
#6
Re: Beginner Programmer Help!
 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.
-
June 30th, 2008, 09:55 AM
#7
Re: Beginner Programmer Help!
...and expensive.
-
June 30th, 2008, 12:26 PM
#8
Re: Beginner Programmer Help!
 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
-
June 30th, 2008, 02:34 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|