General project management : help !!

Started by
10 comments, last by MV 20 years, 7 months ago
I''m having some troubles with VC++ building projects on many files and I would like to know how you guys proceed ? - One class per file ? - one header and source file ? - #ifndef #define ? Please help, I''m bored to struggle with "undefined class" error messages having good #include !
Advertisement
Generally speaking, when using C++, each class is represented by 2 files unless it''s a template. So a non-template class has the declaration in (name).h, and the implementation in (name).cpp, unless you like putting inline implementation in the header, which isn''t suggested for anything more than, for instance, a Get or Set call on one of your member variables. Templates have everything in (name).h due to their nature.

#ifndef, #define, etc, are compile flags. How to use them depends entirely on what you''re trying to do. Think of them as normal code only the code will be executed during compile time, so, for instance, "#ifndef a" will check to see if there is a "#define a" when it tries to compile, and if there is an "a" defined it won''t compile in any of the stuff after the #ifndef. You must be sure to put an #endif at the end of any #ifndef block or you''ll get weird errors.

Generally speaking, #ifndef is used in header files to do the same thing as #pragma once, namely, to make sure the header is only included once.

Ask more detailed questions if that isn''t sufficient.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Generally one class per file. Exception classes are the exception (har har). I minimize singletons as much as possible and use static classes instead.

I exploit the heck out of forward defines mostly because the rules for using them are pretty easy to remember and they slice compile times somewhat.

I use namespaces to avoid name collisions.

For #ifndef #define I be sure to prepend the namespace name to to avoid preprocessor symbols colliding if a chain of headers manages to include the same named file (like System.hpp).

Also every now and then I check out each namespace''s members to make sure it is correct and I''m not introducing symbols that shouldn''t be there.

I try to rebuild all once a day if I''m doing heavy development to kill warnings and make sure it links properly. (warn level at 4) And generally when I get off the computer I make sure the code is at least compiling.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Thx men !
Not a lot of differences between what you explained and how I use to do, execept that I sometimes put many classes in the same .h and .cpp file, base and derived classes for example. Can this be the reason of my troubles ???

I also dont use static classes (dont know yet what it''s used for)
I''m a girl.

Technically putting multiple class declarations in a file won''t hurt anything provided you do the declarations correctly and don''t forget to implement to the correct class. Realistically it can get confusing.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
The one class per header is generally for other people reading your code. It is easiest to jump to the exact class file when debugging. Java actually enforces this I believe.

Dealing with "undefined class" errors can be alleviated by forward declaring the class that it complains about is undefined. If it still complains about the class then you must provide the declaration by #include''ing the header file. With one class per header file there is no question about which file to #include.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by felisandria
I''m a girl.


I''m so sorry !!! )

And what about cross linked references ? Maybe this can also introduce confusion . Does exist a method to solve such troubles cause I''ve also been struggling with this for a while ?




You avoid circular referencing by using forward referencing, which basically looks like

class CClassToForwardDeclare;

class ThisFile''sClass
{
...


The thing to keep in mind there is that if you forward reference you can only use the forward reference class as a pointer.

-fel

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
quote:Original post by antareus
Dealing with "undefined class" errors can be alleviated by forward declaring the class that it complains about is undefined.


Yes, I also tried this. At the end, I had forward declaring + #include and it still didn''t work !!! Amazing...

Is there a way to see the file generated after pre-compilation that is the step, I believe, where the compiler replaces the #include with the file and so on ?

Yes, it is available in the IDE. Go to your project settings, under preprocessor (which is under C/C++) and enable the option to "generate preprocessed file."

I''ve had to do it a few times, its quite large. It will create it in your project directory, so browse there with explorer and look around for it. I believe it is something like "objName.i" where objName is the name of the compilation unit (which is the name of the CPP file, just minus the cpp extension).
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement