Click to See Complete Forum and Search --> : Beginner Programmer Help!


helixed
June 29th, 2008, 05:35 PM
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

Lindley
June 29th, 2008, 05:39 PM
First, you should have used code tags. Code without proper indenting is hard to read. However:


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?

int.i(0);
int i = 0;

It's the same for classes.

helixed
June 29th, 2008, 06:03 PM
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 (http://newdata.box.sk/bx/c/) 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

GCDEF
June 29th, 2008, 06:20 PM
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 (http://newdata.box.sk/bx/c/) 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);

helixed
June 29th, 2008, 06:23 PM
Oh, okay, now I get it. I feel pretty stupid at this point.

Thanks for the help,

helixed

JohnW@Wessex
June 30th, 2008, 04:54 AM
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. :D

jeron
June 30th, 2008, 09:55 AM
...and expensive. :cry:

Paul McKenzie
June 30th, 2008, 12:26 PM
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.

#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:

#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

ch0co
June 30th, 2008, 02:34 PM
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>