Advertisement

having trouble with headers

Started by July 04, 2001 11:45 PM
5 comments, last by Glak 23 years, 5 months ago
I read Stroustrup''s huge book on C++, and I read the chapter on files twice but I can''t seem to figure out how to get the file stuff down. I''ve also tried to download example code that I''ve found and get it to compile but it won''t. It could be that my IDE (CodeWarrior, about a year old, only used the java part until now) is bad but I doubt that they would have messed up on something so important. I want to be able to do two things: make a few simple types with overloaded operators that are generally passed by value. I want an angle class for example. make more complex objects that are generally passed by pointer, such as a monster class. I think I have this figured out. If at all possible I want to put each class in its own file, or with only a couple others. I am not all that concerned with information hiding, namespaces, etc... I mainly just want it to work and not look too ugly, well that stuff would be nice too.
I think a better explanation of the problem is required, unless I overlooked something. It helps to include as specific of a description of the problem as you can, errors etc...

I know only that which I know, but I do not know what I know.
Advertisement
well I''ve tried it a couple of different ways. What I want to be able to do is this:

//A.cpp
#include "header.h";
class A{ /* implementation */ };


//B.cpp
#include "header.h";
class B
{
A a; //want a variable of A here, not a pointer
};


//header.h

/*not sure what goes in this file, tried lots of stuff*/


I assume it is a header issue but it might not be. Or it might just not be possible, but I think it is.
I dunno of this will make a difference, but the #include lines shouldn''t end with a '';''. Might help.

-Matt
Windows has many problems, but they are very easy to fix - goto Start->Run, type command & press enter. Now type format C:
Hello!

Usually you put the declaration of your classes inside ".h-files" and the implementation in ".cpp-files".

//In A.h

class A {
public:
A(int whatever);
void jump();
};

// In A.cpp
#include "A.h"

A::A(int whatever){
//Do some wicked stuff
}

void A::jump() {
//Do some wicked jumping stuff
}

//In B.h
#include "A.h"
class B {
public:
B(A* pointer);

private:
A* aPointer;
};

//In B.cpp
#inlclude "B.h"

B::B(A* pointer) {
this->aPointer = pointer;
}


Hope that clears things up a bit for you...
HAppy coding!!

/Derwiath






------------------------------- Derwiath -
ok I tried that but it doesn''t seem to work. When I tell it to check syntax or precompile either of the header files it says:

Error : declaration syntax error
A.h line 3 class A {

Error : declaration syntax error
A.h line 4 public:

Error : function has no prototype
A.h line 5 A(int whatever);

Error : declaration syntax error
A.h line 7 };


Also your example didn''t answer my second question, you are using a pointer. I want to be able to have actual objects used in other classes and functions. I am almost certain that it is possible, at least that is what the book led me to believe.
Advertisement
The syntax errors sound like the kind of errors that a standard C compiler would generate when faced with C++ code. It appears not to recognize the keywords like class, public etc - are you sure that you''re compiling the .h file in C++ mode. I''m not familiar with Borland C++, but because both C & C++ header files are .h the IDE could concivably be trying with the C compiler. You might want to call you header files .hpp - I think that that is also a valid extension & explicitly says the header is C++.

Yes you can use classes with classes and your code
A a; is fine.

Cheers
Brad

This topic is closed to new replies.

Advertisement