Everyone was talking about other things, so we never noticed the real error.Quote:
Originally Posted by laserlight
Printable View
Everyone was talking about other things, so we never noticed the real error.Quote:
Originally Posted by laserlight
This helped :) Everything is working as intended now, thanks for the help!Quote:
> #ifndef _FUNCTION_H
Pick another name. Everything which begins with _ is reserved, so you should avoid using it yourself.
Interesting. I cannot duplicate that error in Dev-C++.
Really? Are we using the same version?Quote:
Originally Posted by laserlight
Just for clearance:
This is the faulty version:
And the working one:Code:#ifndef _FUNCTION_H
#define _FUNCTION_H
#include <windows.h>
inline void printout(char sentence[], int length, int interval)
{
int x;
for (x=0; x<length; x++)
{
cout<< sentence[x];
Sleep(interval);
}
}
#endif
Code:#ifndef FUNCTION_H
#define FUNCTION_H
#include <windows.h>
#include <iostream>
using namespace std;
inline void printout(char sentence[], int length, int interval)
{
int x;
for (x=0; x<length; x++)
{
cout<< sentence[x];
Sleep(interval);
}
}
#endif
Ah, now I get it. It does look like you have given us a working example where failure to comply with the standard gives you a headache.
Indeed, as i said, very frustrating...Quote:
Originally Posted by laserlight
In general, placing C++ code inside header files ( or .h files ) is bad practice. Header files should only really contain function prototypes and class delcarations. All class and function bodys should be stored in the attached C++ source file (.cpp ) This increases program matienence and makes it clearer your intentions
How do i "attach" a .cpp file to a header file?Quote:
Originally Posted by swgh
You just implement the functions whose prototypes are in the header file, and then compile the source files and link them. If you are using some IDE with some kind of project mechanism, this compilation and linking will all be done for you.
>> Interesting. I cannot duplicate that error in Dev-C++.
Seeing how Dev-C++ supposedly isn't a compiler I don't see how any one could.
Ah, i get it now. Thanks once again.Quote:
Originally Posted by laserlight
Just one clarification though. Identifiers starting by an underscore are reserved for the implementation. Not for the language itself. Hence two compilers behaving differently but still complying to the standard.
And there is an important distinction depending on them being followed by an uppercase letter, another underscore or lowercase letter or digit.
If followed by a lowercase letter or a digit, they are meant to be used by the implementation on the global scope only.
If followed by anything else they can be used by the implementation anywhere.
But since these are guidelines to implementation developers, I think it is perhaps a little too pedantic of some to simply deny access to the declaration of any identifier starting with an underscore.
> Seeing how Dev-C++ supposedly isn't a compiler I don't see how any one could.
Seeing you can't write code with a compiler, I don't see how you could test it otherwise
This is really giving me a headache!
I now separated the function prototype and the body of the function into a header and a .cpp file.
First time around, it worked beautifully, but now im getting all kinds of compiler warnings.
Here are the files:
Main
HeaderCode:#include <iostream>
#include <string.h>
#include "printout.h"
using namespace std;
int main()
{
char sentence1[] = "Still ain't working!";
int length = strlen(sentence1);
printout(sentence1, length, 100);
std::cin.ignore();
}
"attached" source fileCode:#ifndef PRINTOUT_H
#define PRINTOUT_H
inline void printout(char sentence[], int length, int interval)
#endif
expected init-declarator before "using"Code:#include <windows.h>
#include <iostream>
inline void printout(char sentence[], int length, int interval)
{
int i;
for (i = 0, i < length, i++)
std::cout<< sentence[i];
Sleep(interval);
}
}
expected `,' or `;' before "using"
`printout' undeclared (first use this function)
I just wrote the source files with Dev-C++ in the same project as main and the headers, and as i said, it worked perfectly, but now it just won't compile. I even tried closing Dev-C++ and rewriting the entire application.
Btw: If i want to inline a function, do i have to inline it in the prototype or in the actual declaration? Or both perhaps?
The function cpp file should also include the "printout.h". Otherwise you you include printout.h on your main cpp file, the function definition will not exist.
Also you have one extraneous closing brace in your function cpp file