-
December 1st, 2010, 05:35 PM
#1
[beginner] Include problems tidying up code
I've currently trying to convert a little project written in C++ into a more conventional style. I'm learning the very basics of C++ and I thought it would help me to understand. What the original author has done is write a lot of code in header files. He has used them almost exclusively. I thought it would be a good idea to create proper header and .cpp files.
The header I'm looking at right now has no includes or anything. It goes straight into defining the one function that it consists of. When I try to put the prototype of the function in the header, and the actual function into its own .cpp file I get a lot of problems with non included variables (even namespace std).
What was available to the old header when it had the whole function in it? How could the old header have this information and the cpp file I created doesn't?
Just to clarify:
older header had:
void func_name() {
code, no sign of any includes
}
Now:
heade:
void func_name();
cpp file:
void func_name() {
code, no sign of any includes
exact same as previous.h
}
Thanks you for your help.
-
December 1st, 2010, 09:16 PM
#2
Re: [beginner] Include problems tidying up code
Post some code that demonstrates the problem or I don't think anyone can figure out what the problem is.
-
December 2nd, 2010, 10:17 AM
#3
Re: [beginner] Include problems tidying up code
the old header... unless it gets included somewhere... doesn't get compiled. So it can contain just about anything and not cause any problems.
-
December 2nd, 2010, 11:45 AM
#4
Re: [beginner] Include problems tidying up code
It does get included. It's so frustrating working with this sloppy code. I mean I'm no good but even to me this looks like bad news. I thought there might be some logic to it but apparently not. I'll keep investigating other routes...
-
December 3rd, 2010, 07:12 AM
#5
Re: [beginner] Include problems tidying up code
Remember that each cpp file must include it's own header so that the functions can call each other.
I use these macros to keep loops clean and simple.
#define LoopForward(min,var,max) for(var=min;var<=max;var++)
#define LoopBackward(min,var,max) for(var=max;var>=min;var--)
#define LoopForwardStartAndLength(var,start,length) LoopForward(start,var,((start)+(length))-1)
#define LoopBackwardStartAndLength(var,start,length) LoopBackward(start,var,((start)+(length))-1)
#define LoopForwardLengthFromZero(var,length) LoopForward(0,var,(length)-1)
#define LoopBackwardLengthFromZero(var,length) LoopBackward(0,var,(length)-1)
-
December 3rd, 2010, 08:00 AM
#6
Re: [beginner] Include problems tidying up code
I don't see how those macros makes the code cleaner. In my opinion all they do is to make things more obfuscated. Do you have macros like
Code:
#define DIVIDE(x,y) (x/y)
#define ASSIGN(x,y) (x=y)
#define COMPARE(x,y) (x==y)
as well?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|