Namespace in library not declared?

Hey,

ok, here's the problem:
I made a min.cc which define a simple function:
1
2
3
4
5
6
7
8
9
10
11
#include "min.hh"
namespace mylib {
int min (int a,int b){
	if(a<b){
		return a;
		}
	else{
		return b;
		}
	}
}

I also have a corresponding .hh file and I compiled it into a library with:
ar q libMin.a min.o

And I have my program, which basicly tests if this works:
1
2
3
4
5
#include <iostream>
int main(){
	std::cout << mylib::min(3,5) << std::endl;
	return 0;
	}
Compiling it with
g++ -o ex24 ex24.cc -L. -lMin
Gives this error:
ex24.cc:14: error: âmylibâ has not been declared

I have allready tried to fix this, and tried to find why it's not working. But I didn't find anything helpfull.
Please help me, thanks in advance
Last edited on
Is your function declared in the appropriate namespace in the header?
And you're aware of std::min, right?

Edit: you might want to do something about your indentation and brace style too.
You basically have two choices:

1
2
3
4
5
6
7
8
int main()
{
    //code here
}

int main() {
    //code here
}
Last edited on
The first question you should be able to answer yourself with the information given above. But i believe it is.

And you're aware of std::min, right?

No, but is this relevant?
But i believe it is.

Instead of "believing", you'd be better off checking the header or at least post it here, so others can do it for you.

No, but is this relevant?

Obviously, it makes what you're doing obsolete.
closed account (z05DSL3A)
Dunblas wrote:
And I have my program, which basicly tests if this works:
1
2
3
4
5
#include <iostream>
int main(){
	std::cout << mylib::min(3,5) << std::endl;
	return 0;
	}
Missing an include (min.hh)?

Edit: Although with the error reported on line 14, I guess you are missing things out on you post.
Last edited on
That's it, I needed an
#include "min.hh"
in my main programm aswell.

Can someone explain why?, because i don't think both my min.cc and my actual main programm should have this line.
Why doesn't compiling my programm with the library do this?

Although with the error on line 14, I guess you are missing things out on you post.

I also made a "max"-function, which I let out because that's the same problem
Last edited on
You can't use a function in a translation unit before it is declared.
Including min.hh with the function declaration takes care of that.
Whether you pass -lMin on the command line or not has no effect on compiling, this is only relevant for the linker in the final linking step.
Last edited on
Can someone explain why?
A function that uses another functions needs to know how that other function looks like.

Whenever you want to use a function/class... you have to include the definition (the header file)
Topic archived. No new replies allowed.