How to tell C++ to use a different function (with the same name as another function)? -
i have 2 libraries included in program both have same function name, need able use both, need c++ know 1 i'm referring (in places referring 1 or other). reason why i'm doing because making own library , want have names functions, conflicting functions in else's library i've included, , make matters worse, of functions in library use functions in other persons library has same name.
my library .h/.cpp file way. also, when calling functions, don't want luggage such mynamespace::myfunc(). want call myfunc(). however, don't mind calling other persons function using namespace (though don't want modify library in case break something). (i'm new c++ btw)
heres new (test - far) code : not working w/ errors: error c2668: 'myfunc' : ambiguous call overloaded function
main program.cpp
#include "otherslib.h" #include "mylib.h" #include <iostream> using namespace mynamespace; int main(){ std::cout << myfunc() << std::endl; return 0; }
mylib.h
#pragma once namespace mynamespace{ int myfunc(); }
mylib.cpp
#include "mylib.h" namespace mynamespace{ int myfunc(){ return 1; } }
otherslib.h
#pragma once int myfunc();
otherslib.cpp
#include "otherslib.h" int myfunc(){ return 0; }
you should define functions in namespace, , use namespace when calling them.
namespace mynamespace { int myfunc(etc) { ... } } int main() { cout << mynamespace::myfunc(); }
to avoid having specify namespace time, this:
namespace mynamespace { int myfunc(etc) { ... } int main() { // call own myfunc: myfunc(); // call myfunc: ::myfunc(); } }
Comments
Post a Comment