Previous in Forum: I got minor Windows problems   Next in Forum: Toshiba Question
Close
Close
Close
4 comments
Rate Comments: Nested
Member

Join Date: Oct 2006
Posts: 6

Class C++

02/08/2007 5:05 AM

Hi.I have a question.I am studying C++.I need to know why in this program:

#include <stdio.h>

main()

{ ... my_class z; ... }

class my_class { public: int my_func (int a) { /*function definition */ } } ;

I have the error "my_class undeclared, (first use this function)"?

Thanks.

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Guru

Join Date: Jan 2007
Location: Edinburgh, Bonnie Scotland
Posts: 1334
Good Answers: 23
#1

Re: Class C++

02/08/2007 7:11 AM

Try declaring all global functions and variables before the start of the "main" program: this aviods confusion, not only in the pocessing but also in revising the parameters later.

__________________
Madness is all in the mind
Register to Reply
Guru

Join Date: Sep 2006
Posts: 4513
Good Answers: 88
#2

Re: Class C++

02/08/2007 9:57 AM

Master the differences between class declarations vs definitions, and think about how your compiler sees this program. It is reading your program sequentially and when it encounters your application of a class before the class is adequately defined, it's gonna gag. Guaranteed.

Think of a class as a kind of type (which it is, actually). For example, you wouldn't use the typdef'd name of a built-in type before defining your type's alias:

INT8 foo;

foo = 'c';

typedef char INT8;

A class is completely defined once the closing curly brace appears. Once the class is defined, all the class members are known. The size required to store an object of the class is known as well.

It is possible to declare a class without defining it:

class Foo; // Declaration of the Foo class

This declaration, sometimes called a forward declaration, introduces the name Foo into the program and indicates that Foo refers to a class type. After a declaration and before a definition is seen, the type Foo is an incomplete type - it's known that Foo is a type but not known what members that type contains.

An incomplete type can be used only in limited ways. Objects of the type may not be defined. An incomplete type may be used to define only pointers or references to the type or to declare (but not define) functions that use the type as a parameter or return type.

A class must be fully defined before objects of that type are created. The class must be defined - and not just delcared - so that the compiler can know how much storage to reserve for an object of that class type. Similarly, the class must be defined before a reference or pointer is used to access a member of the type.

A class data member can be specified to be of a class type only if the full definition for the class has already been seen. If the type is incomplete (for example, a data member of class type Foo (illegally) declared inside of Foo's class definition), a data member can be only a pointer or a reference to that class type (for example, a data member of type Foo* or Foo& is okay).

Because a class is not defined until its class body is complete, a class cannot have data members of its own type. However, a class is considered declared as soon as its class name has been seen. Therefore, a class can have data members that are pointers or references to its own type. For example (for clarity, both classes that follow are simple classes which contain no function members):

class Band {

public:

char *name; // "Jamiroquai"?

int numberOfCurrentMembers; // 9?

int numberOfFormerMembers; // 11?

char *genre; // "Acid Jazz, Funk, Disco, Pop, Rock"?

char *leadSinger; // "Jay Kay"?

float avgNumberOfDrugOverdosesPerMember; // Who knows?

};

class FunkyBandListNode {

public:

Band band;

FunkyBandListNode *nextBand;

FunkyBandListNode *prevBand;

};

Dig(eridoo)?

-e

PS: CR4's nifty little editor eats leading whitespace. Consequently everything that should be indented (the interior class members, like) are not. This is not my doing.

Register to Reply
Guru

Join Date: Sep 2006
Posts: 4513
Good Answers: 88
#3

Re: Class C++

02/09/2007 10:17 AM

While it is one thing to know, by rote, the rules governing declarations vs definitions, it is far better, I think, to know the Why behind those rules.

Behind even this, one must stand in the compilers "shoes" (so to speak) and look at your code from the compiler's standpoint. While we humans can look at a page of code and quickly gather what the code does - or is supposed to do - a compiler examines each statement, one at a time, and tries to decide if it knows enough by that point to make sense of what it sees.

Try this: take a sheet of paper and cut a slit in it the height and maximum length of one line (assuming here that no expressions are continued on the the next line by means of a backslash '\'). Then, slide the sheet down your screen, one line at a time, and decide whether you, as the compiler, can possibly know what to do with what you see based only on what you have already seen. If you can't, then your compiler probably can't either.

For example, if you, as the compiler, have seen the name of the class at the top of a class definition and are now looking at the definition of a data member inside the class definition, you know that a class having this name exists, but not yet how much storage an object of this class requires (having not yet seen the following members, if any, nor the following functions, if any, before the closing brace). Let's say that so far you've seen:

class Foo {

public:

And now you're trying to figure out what to do with this line:

+----------------------------------------+
| Foo *nextFoo;
+----------------------------------------+

You decide that this is okay. Why? Because the programmer (also you) has declared a pointer to an object of class Foo. You, as the compiler, know how big pointers are, so you know enough about this member to allocate storage for it if need be.

But what if, instead, the line were this:

+----------------------------------------+
| Foo nextFoo;
+----------------------------------------+

You gag. Why? Because you haven't yet seen the closing curly brace for class Foo, and so you don't know how big Foo is. And so you complain because you don't know how much storage to allocate for member nextFoo.

So why can you declare a pointer to an object of class Foo, but not be able to use that pointer until you've seen the full class definition? Because while you, as the compiler, know how big a pointer is (and have no trouble allocating storage for it), you still don't know how to access the object the pointer points to, nor how big that object is.

Why do you need to know how big the pointed-to object is? Because (as a for-instance) if you have several such objects, say, in an array, they will located next to each other in memory. When you go to increment a pointer pointing to one of these objects, you, the compiler, don't know the span of the object (the number of bytes required for its storage, plus some, if padding is required), so you won't know much offset to add to the pointer to make the pointer point to the next thingie. In this sense, a pointer really "contains" two kinds of information: where and how big. So, after a class declaration but before its definition, a pointer to an object of that class type cannot be used because the pointer does not yet know how big the object is.

Not only do you, as the compiler, track where and how big, but also how an object is organized. You cannot allow the use of pointer to an object to an as-yet-not-fully-defined class, because you still don't know how to access all of that class's members. Whenever you, as the compiler, lack even the tiniest detail that would prevent you from fully understanding what to do with what you see, you gag.

I've found that standing in the compiler's shoes is one of the most instructive viewpoints to hold when you're learning a new computer language.

Good luck!

-e

Register to Reply
Power-User

Join Date: Mar 2005
Posts: 214
Good Answers: 3
#4

Re: Class C++

02/09/2007 5:30 PM

You must declare the class before using it.

For example:

class my_class{public: etc. }

void main()

{

my_class z;

.

.

}

This will work!

In your example you are declaring an instance of my_class (my_class z;) before declaring it my_class. The compiler does not know what my_class you are talking about.

Abe

Register to Reply
Register to Reply 4 comments
Copy to Clipboard

Users who posted comments:

amichelen (1); GM1964 (1); user-deleted-13 (2)

Previous in Forum: I got minor Windows problems   Next in Forum: Toshiba Question

Advertisement