C ++模板+共享库=>暧昧的电话

时间:2011-02-10 01:15:33

标签: c++ templates overloading shared

我正在写一个共享库。这个想法如下: 库中的共享函数将使用int或double参数调用。它必须接受两者。在该功能的某一点上,有一个呼叫私人"对参数执行某些操作的函数,具体取决于它是int还是double。 我决定使用模板来实现库函数。如果我理解正确,编译器需要知道参数的类型,否则它无法编译库。因此,我实例化了两个模板,一个用于int,一个用于double。 问题是编译器似乎并不知道应该调用哪个版本的私有函数,尽管它知道它的参数类型。

已经很晚了,我不知道它有什么问题,请帮助我: - )

彼得

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;
}

编译器


../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(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)

3 个答案:

答案 0 :(得分:2)

您对匿名命名空间的使用不正确。您在匿名命名空间中声明printNumber(),然后在全局范围内定义它;这会导致歧义,因为您有两个 printNumber(int)两个 printNumber(double)函数。

试试这个:

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:如你的例子所示。

答案 1 :(得分:1)

删除使用命名空间std; 在核心代码中,声明要使用的std函数,例如:

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

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

,您的程序将正常运行。

答案 2 :(得分:0)

我认为模棱两可的原因是“10”可以是int或double。尝试将其作为一个或另一个投射,看看是否修复它。

相关问题