在 C++ 中使用模板时出现问题

发布于 2024-11-03 11:32:27 字数 1243 浏览 1 评论 0原文

我第一次在 C++ 中使用模板,并在尝试编译时遇到问题。基本上尝试创建我自己的基本 ArrayList 类型:

.hpp:

#ifndef ARRAYLIST_HPP_
#define ARRAYLIST_HPP_

template <class T>
class ArrayList
{
    private:
        int current, top ;
        T * al ;

    public:
        ArrayList() ;   // default constructor is the only one
};

#endif /* ARRAYLIST_HPP_ */

.cpp:

#include "ArrayList.hpp"

using namespace std ;

//template <class T>
//void memoryAllocator( T * p, int * n ) ; // private helper functions headers

template <class T>
ArrayList<T>::ArrayList()
{
    current = 0 ;
    top = 10 ;
    al = new T[top] ;
}

main:

#include "ArrayList.hpp"

int main()
{
    ArrayList<int> test ;
}

当我尝试在没有 main 的情况下构建时,它编译得很好,但是,一旦我尝试在 main 中使用它,我就会得到以下错误:

Undefined symbols for architecture x86_64:
  "ArrayList<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ArrayList()", referenced from:
      _main in Website.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [APProject2] Error 1

任何有关可能出现问题的想法将不胜感激!

干杯!

I am using templates for the first time in C++ and running into a problem when I try to compile. Basically trying to create my own basic ArrayList of sorts:

The .hpp:

#ifndef ARRAYLIST_HPP_
#define ARRAYLIST_HPP_

template <class T>
class ArrayList
{
    private:
        int current, top ;
        T * al ;

    public:
        ArrayList() ;   // default constructor is the only one
};

#endif /* ARRAYLIST_HPP_ */

The .cpp:

#include "ArrayList.hpp"

using namespace std ;

//template <class T>
//void memoryAllocator( T * p, int * n ) ; // private helper functions headers

template <class T>
ArrayList<T>::ArrayList()
{
    current = 0 ;
    top = 10 ;
    al = new T[top] ;
}

The main:

#include "ArrayList.hpp"

int main()
{
    ArrayList<int> test ;
}

When I try to build without the main it compiles fine, however, as soon as I try to use it in the main I get the following error:

Undefined symbols for architecture x86_64:
  "ArrayList<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ArrayList()", referenced from:
      _main in Website.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [APProject2] Error 1

Any thoughts as to what might be the problem would be much appreciated!

Cheers!

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

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

发布评论

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

评论(3

后知后觉 2024-11-10 11:32:27

模板需要在标头中声明并定义。

另外:我认为这不是真正的错误。它提到了 ArrayList 的实例化,我在任何地方都看不到。

此常见问题解答条目解释了原因。

Templates need to be declared and defined in the header.

Also: I don't think that is the real error. It mentions an instantiation of ArrayList<std::string> which I can't see anywhere.

This FAQ entry explains why.

时光无声 2024-11-10 11:32:27

您需要将实现包含在 .hpp 文件中。编译器需要在编译时知道 T 才能生成特化。

You need to include the implementation in the .hpp file. The compiler needs to know T at compile time to generate the specialization.

夏见 2024-11-10 11:32:27

您不能将模板化代码放入单独的 .cpp 文件中...您的构造函数应该位于 ArrayList 标头中。关键是,只有在编译 main() 时,编译器才意识到需要实例化 ArrayList 以及 T 将采用什么类型,因此需要有可用于实例化的代码。 ..

You can't put the templated code into a separate .cpp file... your constructor should be in the ArrayList header. The point is that it's only when main() is compiled that the compiler realises it needs to instantiate ArrayList and what type T will take, and so it needs to have the code available to do the instantiation....

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