Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++ class and C functions

This thread is locked; no one can reply to it. rss feed Print
C++ class and C functions
William Labbett
Member #4,486
March 2004
avatar

Take this situation :

case A : Programming in C :

file1.c includes file2.h

file2.h has a declaration of a function 'function()'

function() is defined in file2.c

I know from experience that file1.c can call function() and the program will compile.

case B : Programming in C++ :

file1.cpp includes file2.hpp

file2.hpp has declaration of a class 'Class'

'Class' is defined in file2.cpp

There is an instance of 'Class' in file1.cpp.

The program will compile with the error : 'Forward declaration of Class Class.

Why I'm Confused :

The situations are the same except a Class has been substituted for a function
and the language changed.

So, why does the second case produce the error mentioned?

pkrcel
Member #14,001
February 2012

It confuses also me, but I'd say check the syntax of the Class instanced in File1.cpp

...otherwise I might have simply misunderstood you. :P

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

William Labbett
Member #4,486
March 2004
avatar

Ironically, the more I try to be unambiguous in my outlines of problems the harder it gets to understand them :-/

The kind of Declaration of an Instance in file1.cpp would be like this :

Class a_class_instance;

Perhaps it's wrong? Does the compiler take that to be a declaration of the actual class itself?

I haven't coded for a long time and even when I did I never really became a knowledgeable programmer because I was knee-deep in a million other game related things.

Peter Hull
Member #1,136
March 2001

I'm not sure of the C++ terminology but if you have an 'empty' declaration like this

class Class;

then you can only do very limited things with it.

Each file needs to see the complete definition of the class to be able to use its members, otherwise you will get the Forward Declaration error.

[edit]

file1.cpp

#include "file2.hpp"

Me* func(Me* me) {
  return me; // OK
}

void other(Me* me) {
  me->something(); // Not OK
}

file2.hpp

class Me;

file2.cpp

class Me {
public:
  void something() {}
};

pkrcel
Member #14,001
February 2012

I guess that William is actually writing a forward declaration and not declaring a global object....

I can't wrap my mind around the syntax and can't check right now....

EDIT: ah, yes, William how do you declare Class in file2.hpp?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

DanielH
Member #934
January 2001
avatar

It is similar to allegro5's headers. The objects are declared like:

typedef struct ALLEGRO_BITMAP ALLEGRO_BITMAP;

In the source that includes only the header file, when you defined an object:

A b the compiler would give you a error because you would not be able to use any of the class' functions. Including the constructor, destructor, functions, or variables. That source file has no idea what the class 'looks' like.

Dizzy Egg
Member #10,824
March 2009
avatar

Not sure if it's relevant, but you can do this:

#SelectExpand
1 2class Human; 3 4void KillHuman(Human *aHuman); 5 6class Human 7{ 8 //...guts 9}; 10 11void KillHuman(Human *aHuman) 12{ 13 //...blah 14}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

torhu
Member #2,727
September 2002
avatar

The kind of Declaration of an Instance in file1.cpp would be like this :

Class a_class_instance;

The compiler needs to know the size of the class to be able to allocate space for it. And the layout if you want to access the fields. And it probably needs to know if there is a default constructor to call. Same thing goes for structs.

To create a function call it, only needs to see the header. That's the difference.

William Labbett
Member #4,486
March 2004
avatar

Right. I see I've missed quite a few posts.

Thanks for all the help guys. I can see now that if a cpp file doesn't know anything
about a Class except what it's called, then it can't make an instance of it or know what to do when any of it's members are refered to.

Thanks. I'll put my class definitions in the headers :)

beoran
Member #12,636
March 2011

Or you can use the PIMPL idiom to get back a semblance of encapsulation. Or inherit from an abstract class behind the scenes.

http://en.wikipedia.org/wiki/Opaque_pointer#C.2B.2B

Or, just keep using good old C, where encapsulation is easy and free.

bamccaig
Member #7,536
July 2006
avatar

Go to: