issue with header file

Pages: 12
closed account (4Gb4jE8b)
i'm trying to make a header file called timed_text.h, which looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef TIMED_TEXT_H
#define TIMED_TEXT_H

#include <iostream>
#include <string>
#include <time.h>

void wait ( double seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

void timed_text(const std::string content, double time)
{
	int length = content.length();
	for(int i = 0; i<=length;i++)
	{
		char a = content[i];
		std::cout << a;
		wait(time);
	}
}

#endif 


And it runs well if i call timed_text(string,double) until the last character. At which point it give me the error in the file xstring: string subscript out of range.

However when i define it as a function in a cpp file, it works just fine. any ideas why this is happening?
use < instead of <=.
read the help tutorial on strings.
closed account (4Gb4jE8b)
oh trust me i have, i actually did figure out the problem, and it actually wasn't that. It's that my header was saving to one place, when the header i was including was elsewhere. Sorry for the stupid problem :P
Why do you have these function definitions in a header file?
closed account (4Gb4jE8b)
to be able to easily re-use them mostly. Is it a bad thing to do? If so (which by your question i have a feeling it is), please explain why, I'd be happy to learn :)
The header should only contain prototypes; put the implementation in a source file.
closed account (4Gb4jE8b)
Well i guess i understand that, but again why? Or could you at least point me to some reading that explains why?
Heyas!

You see, header files are copy-and-pasted into the place where they are #include d (that's what the directive does!). If you post implementations that translate into compiled code, then when your compiler tries to combine your two files via linking, you'll get redefinition errors.

-Albatross
closed account (4Gb4jE8b)
@Albatross, oooooohhhhhhhhhh :P Alright I understand, but does that truly mean it's a bad thing as long as you don't redefine the header implementation? What I mean is, if you just call the function, and never redefine the function, is there any harm done? Why is it that we often post the function definition after it's declaration?
In another threat (that now I can't find):
Alice: Why I should not include .cpp ?
Bob: Because it kills the purpose of having a linker. If you modify the main.cpp, you will need to recompile the included cpp files (that did not change), ending with higher compilation time.
Last edited on
closed account (4Gb4jE8b)
... but i'm not including a cpp? Every cpp in a project is compiled unless not changed, by including it in another cpp, and if that second cpp changes, the compiler would recompile the first cpp because (i'm now presuming) of the copy paste effect albatross mentioned.

but this is a constant header file with a constant definition of a function. the function is called in the cpp as if defined in the cpp itself, but it's not, it's defined in the header as some function like cout or cin would be. Increasing the overall organization of the code (if you comment about where this strange header came from and why it's there at least).... at least in theory.

Am i wrong? again, if so, please explain. I'm liking this thread for how much it's teaching me.

Why is it that we often post the function definition after it's declaration?

what i meant being that we often do this
1
2
3
4
5
6
7
8
9
void doSomething();
int main()
{
   doSomething();
}
doSomething()
{
   //definition
}


instead of this:

1
2
3
4
5
6
7
8
void doSomething()
{
   //definition
}
int main()
{
   doSomething();
}
Last edited on
i know that often times when i post code i put the classes and the definitions in main cpp file to make things easier to read rather then break up the code for every file. that way someone can just copy and paste to run it without hassle. i know im not the only one.
In int main(){ is all the logic of the program. I just expect to be the "first" thing I see.
Besides if you put only the prototypes you will immediately know what functions you have, how you must call them, and you get an idea of what they do.
The other method will work, just adopt a convention.

headlessgargoyle: ... but i'm not including a cpp?
Bob: irrelevant. You are including the definitions of your functions.
headlessgargoyle: but this is a constant header file with a constant definition of a function.
Bob: Yeah, and you are compiling it over and over again when you could just generate a object code that will be latter linked.

If you are interested read something about makefiles.

btw: rocketboy9000 was right. Arrays go from 0 to size-1, but you are trying to access content[length] that is out of bounds. Your program didn't crashed because you were unlucky.
Last edited on
@spinelessbasilisk (whoops)
Having the header define the implementations kills any possibility of #including the header in two or more C++ files in your project. Remember: all #include does is copy-and-pastes. If you have the same definition in two different files, that causes a redefinition error. Sorry. :P

And posting the definition after the declaration at least in the main C++ file is just so that you don't have to scroll down through tons of code to get to main(). Generally, functions are either long sections of code, or shorter pieces of code that get used a lot. :)

Does that answer your question?

EDIT: ne555/Bob is right.

-Albatross
Last edited on
objects in an object oriented language.. go figure
closed account (4Gb4jE8b)
@albatross, though i don't understand the reference to spinelessbasilisk, that answers my question perfectly (or at least i understood your answer better than the others). thank you. and you answered the question about why we put declarations after main () in a matter that i understood too.

@ne555, i'm not 100% on what you're saying, but i don't quite understand linkage yet. And i shall look into makefiles as well.

@rocketboy9000/ne555, i'm unsure why it's working on my computer.... but it is, so i dunno :P I use MSVS 2010, if that makes a difference. EDIT: but i changed it to what you said to be safe.

@cranium :P

@ all, thanks for all your help, and thanks for getting me to over 100 posts :P
Last edited on
http://www.cplusplus.com/reference/string/string/c_str/ I guess that your compiler (I'm not sure if it's system dependent) put an '\0' in s[s.size()]. However you should not touch it.

If you want to avoid the pulling polling in your wait function http://www.cplusplus.com/forum/unices/10491/#msg49054

-74ls244
Last edited on
closed account (4Gb4jE8b)
isn't my form of a wait (which i didn't write granted) function more portable though? all it requires is time.h, which is standard isn't it?
yes, but clock() measures only the time used by your app, so, if you're running crysis in the background, the wait time will be different. Usually it's best to stop your process and get the OS to resume it after a pause.
closed account (4Gb4jE8b)
ahh. I see. interesting. *walks off whistling*
Pages: 12