开发者

C++ Templates + Shared Library => ambiguous call

I am writing a shared library. The idea is as follows: Shared function inside the library will be called with either int or double parameter. It must accept both. At one point in the function there is a call to a "private" function that does something with the parameter depending on whether it is an int or double. I decided to use a template for the library function. If I understand it correctly the compiler needs to know the parameter's type otherwise it could not compile the library. I therefore instantiate two templates, one for int and one for double. The problem is that the compiler doesn't seem to know which version of the private function should be called, although it knows the type of its parameter.

It's late at night, I don't know what could be wrong with it, please help me :-)

Petr

library.hpp


#include < iostream >

namespace {

void printNumber(int const n);
void printNumber(double const n);

}

namespace library {

template < typename T >
void doSomething(T const number);

}

library.cpp


#include "library.hpp"

using namespace std;

void printNumber(int const n) {
    cout << "This was an int." << endl;
}

void printNumber(double const n) {
    cout << "This was a double." << endl;
}

template < typename T >
void library::doSomething(T const number) {
    // ...
    // Do something that does not depend on T at all...
    // ...
    printNumber(number);
}

template void library::doSomething(int const number);
template void library::doSomething(double const number);

Main.cpp


#include "library.hpp"

int main(int const argc, char const * (argv) []) {
    library::doSomething(10);
    library::doSomething(10.0);
    return 0;
}

Compiler


../src/library.cpp: In function ‘void library::doSomething(T) [with T = int]’:
../src/library.cpp:21:52:   instantiated from here
../src/library.cpp:18:2: error: call of overloaded ‘printNumber(const int&)’ is ambiguous
../src/library.cpp:5:6: note: candidates are: void printNumber(int)
../src/library.cpp:9:6: note:                 void printNumber(double)
/home/petmal/Eclipse_Projects/library/include/library.hpp:6:6: note:                 void::printNumber(double)
/home/petmal/Eclipse_Projects/library/include/library.hpp:5:6: note:                 void::printNumber(int)
../src/library.cpp: In function ‘void library::doSomething(T) [with T = double]’:
../src/library.cpp:22:55:   instantiated from here
../src/library.cpp:18:2: error: call of overloaded ‘printNumber(const double&)’ is ambiguous
../src/library.cpp:5:6: note: candidates are: void printNumber(int)
../src/library.cpp:9:6: note:                 void printNumber开发者_如何学C(double)
/home/petmal/Eclipse_Projects/library/include/library.hpp:6:6: note:                 void::printNumber(double)
/home/petmal/Eclipse_Projects/library/include/library.hpp:5:6: note:                 void::printNumber(int)


Your use of the anonymous namespace is incorrect. You're declaring printNumber() in an anonymous namespace, but then defining it in the global scope; this leads to ambiguity because you have two printNumber(int) and two printNumber(double) functions.

Try this:

library.hpp:

#ifndef LIBRARY
#define LIBRARY

namespace library {
    template < typename T >
    void doSomething(T const number);
}

#endif

library.cpp:

#include <iostream>

#include "library.hpp"

using namespace std;

namespace {
    void printNumber(int const n) {
        cout << "This was an int." << endl;
    }

    void printNumber(double const n) {
        cout << "This was a double." << endl;
    }
}

template < typename T >
void library::doSomething(T const number) {
    // ...
    // Do something that does not depend on T at all...
    // ...
    printNumber(number);
}

template void library::doSomething<int>(int const number);
template void library::doSomething<double>(double const number);

main.cpp: as in your example.


remove using namespace std; in the core code, declare which function of std you want to use, example :

int main() {
using std::cout;
using std::cin;
using std::endl;

//................. your code
}

and your program will run normally.


I believe that the ambiguity comes from the fact that "10" can be either an int or a double. Try casting it as one or the other and see if that fixes it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜