函数模板链接错误

发布于 2024-12-23 04:48:51 字数 939 浏览 0 评论 0原文

我创建了一个函数模板,允许我获取任何数据类型的数据,但在编译时收到错误消息:

Undefined symbols for architecture i386:
  "bool Json::getData<double>(double, Json&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, DataType)", referenced from:
      Coupon::initCoupon(int const&, Json&)in libkuapay.a(Coupon.o)
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
scons: *** [kuaposgw] Error 1
scons: building terminated because of errors.

该函数声明为:

 template < class T> static bool getData(T data, Json &jsonObject, const string &key, DataType dataType);

并调用为:

 Json::getData (couponList[cpnCnt].discount, couponReader, "discount", realType);

其中 couponList[cpnCnt].discount是一个双。

代码本身在我的“内部”目录中编译得很好,但我在上面收到了错误消息 “outer”目录,后者本质上是内部代码的包装器。

I've created a function template that allows me to get data for any data type but am receiving the error message on compilation:

Undefined symbols for architecture i386:
  "bool Json::getData<double>(double, Json&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, DataType)", referenced from:
      Coupon::initCoupon(int const&, Json&)in libkuapay.a(Coupon.o)
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
scons: *** [kuaposgw] Error 1
scons: building terminated because of errors.

The function is declared as:

 template < class T> static bool getData(T data, Json &jsonObject, const string &key, DataType dataType);

and called as:

 Json::getData (couponList[cpnCnt].discount, couponReader, "discount", realType);

where couponList[cpnCnt].discount is a double.

The code itself compiles fine in my "inner" directory but I get the error message above in the
"outer" directory, where the latter is essentially a wrapper of the inner code.

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

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

发布评论

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

评论(2

醉态萌生 2024-12-30 04:48:51

模板的当前状态通常要求您在有函数声明的地方有函数定义。

模板的工作方式是,编译器基本上为模板参数的每个变体生成函数的自定义版本。由于编译器无法提前知道所有这些不同的模板参数是什么(它是 int 或 double 还是在其他文件中声明的某种未知类型? )在调用该函数之前它无法创建这些版本。

这意味着当您调用该函数时,整个函数定义必须可供编译器使用。为了实现这一点,您应该将函数定义放在标题中。

还有其他方法可以做到这一点。类模板的显式实例化。声明一个重载,其中函数没有模板参数。但一般来说,整个模板定义必须位于头文件中。

The current state of templates generally requires that you have the function definition right there where you have the function declaration.

The way templates work, the compiler basically manufactures a custom version of your function for each variation on template arguments. Since the compiler can't know in advance what all those different template arguments will be (will it be int or double or some unknown type that's been declared in some other file?) it can't create those versions until the function is called.

This means that the whole function definition has to be available to the compiler when you call the function. In order to make that happen you should put the function definition in a header.

There are other ways of doing this. Explicit instantiation for class templates. Declaration of an overload in which there are no template arguments for a function. But in general, your whole template definition has to be in a header file.

自此以后,行同陌路 2024-12-30 04:48:51

模板在 C++ 中不会自动实例化,而是在隐式使用或显式实例化时进行实例化。当使用模板时,当模板实例化可用时(例如,通过将其放入头文件中),您可以通过使用该函数来触发前一种情况,就像@Omnifarious所描述的那样。

作为替代方案,您可以使函数成为非静态函数并在源文件中显式实例化它:

template bool getData<double>(double data, Json &jsonObject, const string &key, DataType dataType);

Templates are not instantiated automatically in C++, but rather when used implicitly or instantiated explicitly. You can trigger the former case by using the function when the template instantiation is available when the template is used (e.g. by putting it in the header file), like @Omnifarious described.

As an alternative, you can make the function non-static and explicitly instantiate it in a source file:

template bool getData<double>(double data, Json &jsonObject, const string &key, DataType dataType);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文