What's wrong with my header?

I'm so very confused as to what I've done wrong here guys, hope someone can help.

I've declared a function in a header called: hex_length_convert.h

the .h file is as follows:

string hex_size_convert(string a, string b)

{
string first, second;

first=a;
second=b;
int size_of_second=second.size();
for (int i=0;i<size_of_second;i++)
{
first[(first.size()-second.size())+i]=second[i];

}

return first;

}

I then write #include "hex_length+convert.h" in my main and call the function but I get a bunch of errors regarding the function declaration-is there an obvious issue here that someone much smarter than myself can see?

Anyone know whats wrong here?
What is the purpose of this function?

and

#include "hex_length+convert.h

shoudl read

#include "hex_length_convert.h"

also in hex_length_convert.h

have you included the appropriate directives? are you using the "using std::string" ? or "using namespace std;"
Last edited on
Try copying/pasting the function declaration into your main.cpp. Now do you still have problems?
It's very odd and its purpose isn't really relevant to my problem since it works perfectly when I declare everything in the one main.cpp file, but I'll explain anyway in the hope you can help me.

I am simulating a MMU software element. It has to take CPU data as it would be received from the bus and process this in the correct format. I can produce hexadecimal numbers but the format the bus uses is anything from 8 bits upwards. So, the hex numbers I produce with my code such as the number 255 which is 0xFF would need to be represented as 0x000000FF in order to be a correct implementation for an 8 bit hex number.

That's what this function does basically. It figures out the format I need to adjust my hex strings to.

Now, does that make it clearer what I've done wrong? I hope so :)
Last edited on
it works perfectly in main. The problem is that I need to build an Object Oriented solution. I realize this isn't in a class but this is the first step, just trying to get the header to work at the moment!
George, my header looks exactly as I've written it. It contains only the fucntion definition with no headers included in there itself. I haven't written "using namespace std" there or anything. Is this required? I'm a little confused as to how headers work sorry. I thought they were literally copied into the main/wherever they are included and were just used to make the program more manageable?
Just added the "using namespace std;" line to my header and it works! :)

Thanks very much for your help! Now, can anyone explain to me what that line does exactly? I'm very clueless as to it's function. Also, is it something that will now be defined twice since I've included that header in my main? Should I be using some sort of "guard" against multiple "declarations" of "using namespace...."?

Thanks again! :)
I thought they were literally copied into the main/wherever they are included and were just used to make the program more manageable?


That's correct.

Now, can anyone explain to me what that line does exactly?

Indicates to the compiler that it should always look in the std namespace when look for something. The alternative is to explicitly state the namespace:

1
2
3
4
5
#include <string>
string a; // Never heard of string - error
std::string b; // Ah, the string from the std namespace
using namespace std; // Right, from now onwards, look in std namespace by default
string c; // Now this works 
thank you Moschops! But what is a "namespace" exactly?
Topic archived. No new replies allowed.