Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C++: Two functions with same name dilema  (Read 10013 times)

0 Members and 1 Guest are viewing this topic.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #15 on: April 09, 2013, 09:05:32 am »
Why am I getting errors.. Here's the code I'm using. I have two simple test libraries included: lib1.h and lib2.h

main.cpp
#include "lib1.h"
#include "lib2.h"
#include <iostream>

using namespace LIB1;

int main(){
        std::cout << func() << std::endl;
        return 0;
}

lib1.h
#pragma once

namespace LIB1{
        int func(void);
}

lib1.cpp
#include "lib1.h"

namespace LIB1{
        int func(){
                return 1;
        }
}

lib2.h
#pragma once

int func(void);
 

lib2.cpp
#include "lib2.h"

int func(){
        return 0;
}
 
« Last Edit: April 09, 2013, 09:07:11 am by tom64 »
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #16 on: April 09, 2013, 09:08:16 am »
using namespace LIB1;
DON'T. USE. IT. :D

Also Lib2 is doing it wrong. If it's one of your library, you should change it and let it use its own namespace. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #17 on: April 09, 2013, 09:08:49 am »
eXpl0it3r, if that code works, then can't I just do 'using namespace exp' which would answer the very question everyone was telling me I couldn't do...???
Newbie to C++ and SFML

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #18 on: April 09, 2013, 09:10:54 am »
Quote
Also Lib2 is doing it wrong. If it's one of your library, you should change it and let it use its own namespace.

AAHAHAAARRRGGHH IM SO CONFUSE.

But lib2 is an example of someone elses library that has a function. lib1 is an example of my library where I force a namespace... ????????????????????????

The point of this topic was to ask how to make a function that has the same name and use it without namespace::
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #19 on: April 09, 2013, 09:11:19 am »
eXpl0it3r, if that code works, then can't I just do 'using namespace exp' which would answer the very question everyone was telling me I couldn't do...???
If both libraries use their own namespace (like exp and std do), then you potentially could do that, BUT you shouldn't be using namespace and the code posted by you, the second library doesn't have its own namespace.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #20 on: April 09, 2013, 09:13:09 am »
To be clear: writing "using namespace LIB1" cancels the whole namespace LIB1, and you end up in the exact same situation: all LIB1 functions are again in the global namespace. That doesn't solve any conflict.

You won't solve your problem with such tricks. You have to modify you whole code to use namespaces (for your own functions) properly. There's no other choice.
Laurent Gomila - SFML developer

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #21 on: April 09, 2013, 09:14:25 am »
Quote
and the code posted by you, the second library doesn't have its own namespace.

Yes... but isn't that exactly what many libraries do? Like in the <math> library you just simply use the function 'round()' as is. You don't need a namespace::..

That's why I'm using that as an example.

So in other words, I can't make my own library with simple function names, because I will hit someone elses and therefore need to put ugly xxx:: in front of everything..  :'(
« Last Edit: April 09, 2013, 09:16:23 am by tom64 »
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #22 on: April 09, 2013, 09:15:10 am »
AAHAHAAARRRGGHH IM SO CONFUSE.
If you decalre those two functions:
namespace exp
{
    void funct() {}
}

void funct {};
Everything works fine, since the one funct is in the exp namespace and the other funct is in the global namespace, BUT if you now go and do:

namespace exp
{
    void funct() {}
}

void funct {};

using namespace exp;
You're essentially pulling the first function from the exp namespace into the global namespace, BUT there exists already a function called funct in the global namespace -> name clashing.
So don't use using namespace and you're fine. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #23 on: April 09, 2013, 09:18:13 am »
Quote
That's why I'm using that as an example.
But those are C era functions, it's a bad example.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #24 on: April 09, 2013, 09:20:59 am »
All C functions are brought into the std:: namespace if you include the C++ version of the header (<cxxx> instead of <xxx.h>). And guess why they did that... to solve your problem ;)
Laurent Gomila - SFML developer

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #25 on: April 09, 2013, 09:21:06 am »
I still dont get why it's possible to namespace my functions, but not possible to namespace someone elses functions?

Can't I just wrap the #include "lib2.h" inside a namespace?

That way, my function will be global, and the other (<math>) will be under a namespace?
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #26 on: April 09, 2013, 09:22:35 am »
The external library was not compiled with the additional namespace that you want to add to its headers. So if you do that, the linker will complain about every function of this library ('unresolved external symbol: xxx::round').

It can work only if everything is inlined (i.e. the full code is in the headers, there's no compiled library).
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #27 on: April 09, 2013, 09:22:59 am »
When you're within a namespace, you'll be using the function within this namespace, if you need the global function name, you'll have to use ::funct(). Here's an example with 3 different foo functions.

#include <iostream>

void foo()
{
        std::cout << "Hello" << std::endl;
}

namespace exp
{
        void foo()
        {
                std::cout << "eXpl0it3r says: Hello" << std::endl;
        }

        void bar()
        {
                foo();
        }

        void bar2()
        {
                ::foo();
        }
}

namespace hax
{
        void foo()
        {
                std::cout << "Tank is hax!" << std::endl;
        }

        void bar()
        {
                foo();
                exp::foo();
                ::foo();
        }
}


int main()
{
        foo();
        exp::foo();
        exp::bar();
        exp::bar2();
        hax::foo();
        hax::bar();

        std::cin.get();
}
 

Output:
Hello
eXpl0it3r says: Hello
eXpl0it3r says: Hello
Hello
Tank is hax!
Tank is hax!
eXpl0it3r says: Hello
Hello

If you still don't get it, then I might not understand, what you don't understand... :-\
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #28 on: April 09, 2013, 09:26:52 am »
I totally understand!!!

I just don't get why they never added a feature to C++ to say:
"we are going to rename the function round() from <math> to math_round() only in 'this' .cpp script"

What if someone elses library had the same function name and the same namespace name? It just seems like a clumsy design for a language...

PS: Thanks very much eXpl0it3r for that example, that helps alot.

I suppose I'll just have to bite the bullet. Either create unique function names, or add a namespace to them.

Thanks all for your help.  :) :'(
« Last Edit: April 09, 2013, 09:30:53 am by tom64 »
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #29 on: April 09, 2013, 09:30:40 am »
I just don't get why they never added a feature to C++ to say:
"we are going to rename the function round() from <math> to math_round() only in 'this' .cpp script"
You're using the wrong header, as stated before.
It should be: #include <cmath>, which puts everything into the std namespace. ;)

PS: Thanks very much eXpl0it3r for that example, that helps alot.
You're welcome. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/