C++模板 +共享库=>暧昧的称呼

发布于 2024-10-17 06:03:03 字数 2569 浏览 0 评论 0原文

我正在写一个共享库。想法如下: 库内的共享函数将使用 int 或 double 参数调用。它必须接受两者。在函数中的某一时刻,有一个对“私有”函数的调用,该函数根据参数是 int 还是 double 来对参数执行某些操作。 我决定使用库函数的模板。如果我理解正确,编译器需要知道参数的类型,否则它无法编译库。因此,我实例化了两个模板,一个用于 int,一个用于 double。 问题是编译器似乎不知道应该调用哪个版本的私有函数,尽管它知道其参数的类型。

已经是深夜了,我不知道出了什么问题,请帮助我:-)

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

编译器


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2024-10-24 06:03:03

您对匿名名称空间的使用不正确。您在匿名命名空间中声明 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:如您的示例所示。

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.

再可℃爱ぅ一点好了 2024-10-24 06:03:03

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

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

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

您的程序将正常运行。

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.

蓝天白云 2024-10-24 06:03:03

我认为歧义来自于“10”可以是 int 也可以是 double 的事实。尝试将其转换为其中之一,看看是否可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文