c++ ubuntu linux .cpp 中的模板

发布于 2024-11-06 15:04:32 字数 654 浏览 0 评论 0原文

我目前在 Ubuntu9.10 - c++ 中工作。 我需要在方法中定义一个通用对象。我必须在 .h 文件中定义该方法。我该怎么做呢?我执行了以下操作:

file.h

class ana

{
//code
public:
template <class T>  bool method (T &Data);
};

file.cpp

//code

template <class T>
bool ana::method(T &Data)
{
//code
}

我创建了 .a 文件。

test.cpp 中:

//code
main()
{
    //code
    Ana* ann = new Ana();

    if (ann->method(*ann)){//code}
}

使用 g++ test.cpp -o test libfile.a 编译后出现错误:对 bool.... 的未定义引用代码> 为什么?还有另一种方法来创建通用对象吗?

I am currently working in Ubuntu9.10 - c++.
I need to define an generic object in a method. I have to define the method in .h file. How can I do it? I did the following:

file.h

class ana

{
//code
public:
template <class T>  bool method (T &Data);
};

file.cpp

//code

template <class T>
bool ana::method(T &Data)
{
//code
}

I've created the .a file.

In test.cpp:

//code
main()
{
    //code
    Ana* ann = new Ana();

    if (ann->method(*ann)){//code}
}

After compiling with g++ test.cpp -o test libfile.a I have the error: undefined reference to bool.... Why? Is there another way to create a generic object?

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

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

发布评论

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

评论(5

尸血腥色 2024-11-13 15:04:32

通常的问题。看一下: http://www.parashift.com/c++ -faq-lite/templates.html#faq-35.13

只需将所有内容放入头文件中即可。

file.hpp

class ana
{
    //code
public:
    template <class T>  bool method (T &Data)
    {
        //code
    }
};

将其包含在您的 main.cpp 中,它应该可以正常工作。

#include "file.hpp"
main()
{
    //code
    Ana* ann = new Ana();

    if (ann->method(*ann)){//code}
}

The usual problem. Have a look: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13.

Just put everything into a header file.

file.hpp

class ana
{
    //code
public:
    template <class T>  bool method (T &Data)
    {
        //code
    }
};

Include this in your main.cpp and it should work fine.

#include "file.hpp"
main()
{
    //code
    Ana* ann = new Ana();

    if (ann->method(*ann)){//code}
}
难得心□动 2024-11-13 15:04:32

将函数定义与声明放在一起(在头文件本身中)绝对有帮助,并且是选择的方式。但是,如果您想要分隔函数定义,请在 .cpp 文件末尾添加以下行。它称为显式实例化。这将处理链接器错误。我尝试了一些代码,根据您给出的内容,这似乎有效:

file.h:

#ifndef __ANA_H__
#define __ANA_H__

template <class T>
class ana {

  public: 
    bool method(T& data);
};

#endif

file.cpp:

#include <ana.h>
#include <iostream>
using namespace std;

template <typename T>
bool ana<T>::method(T& data) {
  cout << "Data = " << data << endl;
  if(data > 0) {
    return true;
  }
  return false;
}

//explicit instantiation for avoidance of g++ linker errors.
template
bool ana<int>::method(int& data);

template
bool ana<double>::method(double& data)

使用此方法的缺点之一是对于您希望此函数支持的每种数据类型,都必须包含这些行。因此,现在该方法函数将仅针对 intdouble 运行。代码的规范应该是这样的,即永远不会为上述以外的数据类型调用该方法。
HTH,
斯里拉姆

Putting function definitions together with declarations (in the header files itself) definitely helps and is the way of choice. But, in case you want to separate the function definitions, have the following lines at the end of the .cpp file. Its called explicit instantiation.This will take care of linker errors. I tried some code, and this seems to work, based on what you have given:

file.h:

#ifndef __ANA_H__
#define __ANA_H__

template <class T>
class ana {

  public: 
    bool method(T& data);
};

#endif

file.cpp:

#include <ana.h>
#include <iostream>
using namespace std;

template <typename T>
bool ana<T>::method(T& data) {
  cout << "Data = " << data << endl;
  if(data > 0) {
    return true;
  }
  return false;
}

//explicit instantiation for avoidance of g++ linker errors.
template
bool ana<int>::method(int& data);

template
bool ana<double>::method(double& data)

One of the downsides of using this method is that these lines will have to be included for every data type that you want this function to support. So, now the method function will run ONLY for int and double. The specs for your code should be such that method is never called for data types other than the above.
HTH,
Sriram

通知家属抬走 2024-11-13 15:04:32
  1. 您忘记将 file.cpp 构建到二进制文件中。
  2. 无论如何,您应该将函数模板定义放在标头中。
  1. You forgot to build file.cpp into the binary.
  2. You should put the function template definition in the header, anyway.
横笛休吹塞上声 2024-11-13 15:04:32

您需要更改头文件中 method() 方法的定义:

class ana
{
public:
    template <class T>
    bool method (T &Data)
    {
        // Do whatever you want in here
    }
};

查看以下链接以获取详细说明 - http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

You need to change the definition of your method() method to be in the header file:

class ana
{
public:
    template <class T>
    bool method (T &Data)
    {
        // Do whatever you want in here
    }
};

Check the following link out for a detailed explanation - http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

蒲公英的约定 2024-11-13 15:04:32

只需在 main.cpp 中包含 cpp 文件,如下所示:#include "ana.cpp" 并且不会出现错误。

Just include cpp file in main.cpp like this: #include "ana.cpp" and you will not have errors.

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