# Make header files in c++ when you come up with simple ideas!!

This topic is 4205 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
It is unwise to put "using namespace std;" in a header file since you then force any other files that include your header file to have the contents of std dumped into their namespace.

Really, you would be better to split the above into a .h and a .cpp file, with the function prototype in the .h and the actual implementation into the .cpp file. You could then put "using namespace std;" at the top of the .cpp file without having to worry about affecting other units that use your code.

I appreciate that you are only just beginning, but since you seem to be here offering advice, you need to ensure that you are not suggesting bad practices to people who may have less experience than you.

##### Share on other sites
Quote:
 The most fun part is, if you created a function in a separate source file, you would have to make a function prototype in all the source files that used the function.

Not really. The proper way is to declare the function prototype in MyUnit.h, and define the function in MyUnit.cpp. To use a custom function, for example 'void Foo()', in another source file, you just include MyUnit.h and link to MyUnit.o(if you add MyUnit.cpp to your project, that happens automatically). Your way is mostly incorrect. Just try to include your .h file that contain function definitions in more than 1 source file in the same project. You will get a multiple definition error from the linker, since the function Foo() is defined in all object files that are linked together.

I appreciate your enthusiasm, but it would be better if you wait until you have sorted things out yourself before deciding to tutor beginners. I'm sure you don't want anyone to learn bad practices from you. It's bad karma :)

##### Share on other sites

#ifndef REVERSESTR_H#define REVERSESTR_H#include <string>inline std::string RevStr(const std::string& in){	std::string reversed;	std::string::size_t len = in.length();	for(int i = 0; i < len; ++i)	{		reversed += in;	}	return reversed;}#endif//REVERSESTR_H

However, I'd actually just use the following line to achieve what you're trying to do (note: haven't tested this so it might not be 100% accurate):

std::string in = "Hello, World!";std::string rev(in.rbegin(),in.rend());

You could do one step better and look into Boost.StringAlgorithms

##### Share on other sites
Thank you guys!!!
See I wasn't a looser after all. An I really hope that beginners who go through my
entire comments(filed by you guys) so that even they learn the rigth way.
Again guys than you vey much!! I really appreciate it.

##### Share on other sites
Quote:
 Original post by daeridJust to adopt the helpful viewpoint here, try this refactoring:...You could do one step better and look into Boost.StringAlgorithms

Either way, you're making it much too complicated [smile]

##### Share on other sites
Quote:
 Original post by AbhinashSee I wasn't a looser after all.

Oh, you definitely got the right idea: as you start solving common problems, gathering your solutions into a library rather than cutting and pasting is definitely the way to go. However, you need to make sure that there isn't a better way to do it (e.g. using the standard C++ library) and that your code is of good quality. This is the kind of things that come with experience.

##### Share on other sites
Aww, aren't new programmers cute?

Also, don't abreviate names. Never sacrifice readability to save keystrokes.

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by daeridJust to adopt the helpful viewpoint here, try this refactoring:...You could do one step better and look into Boost.StringAlgorithms

Either way, you're making it much too complicated [smile]

Not if you need a reversed copy of the string. I was going to post reverse(), but then I realized that his function was returning a new string.

##### Share on other sites
Oh, well that's easy too:

std::string reversed_string(const std::string& input) {  return std::string(input.rbegin(), input.rend());}