Archived

This topic is now archived and is closed to further replies.

MV

General project management : help !!

Recommended Posts

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 !

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?




Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites