未灭时间 发表于 2018-3-14 13:26:09

问一下c++模板类的调用问题

#pragma once
#include<iostream>
template<class T>
class LinearList
{
public:
        LinearList(int size);
        ~LinearList();
        void GetElem(T * l, int i, T * e);
protected:
        T *data;
        int length;
        int maxsize;
};
#include"list.h"
#include<iostream>
template<class T>
LinearList<T>::LinearList(int size)
{
        maxsize = size;
        length = 0;
        data = new T;
        if (data == 0)
        {
                std::cerr << "存储分配错误!"; << std::endl;
                exit(1);
        }
}
template<class T>
LinearList<T>::~LinearList()
{
        delete[] data;
}
//template<class T>
// void LinearList<T>::GetElem(T *l, int i, T *e)
//{
//        *e = l;
//}
#include"list.h"
#include<iostream>

int main(void)
{
        LinearList list;
       
        return 0;
}

未灭时间 发表于 2018-3-14 13:27:34

不能像普通类一样用 LinearList list;声明 请问要用怎样的格式声明

BngThea 发表于 2018-3-14 13:50:31

声明的时候需要指明用哪种类型的数据,比如
LinearList<int> list; //对应T为int类型

未灭时间 发表于 2018-3-14 17:03:01

BngThea 发表于 2018-3-14 13:50
声明的时候需要指明用哪种类型的数据,比如
LinearList list; //对应T为int类型

还是不行

BngThea 发表于 2018-3-14 17:15:00

未灭时间 发表于 2018-3-14 17:03
还是不行

你没有写默认构造函数,只写了一个带一个参数的构造函数,实例化的时候需要传初值
LinearList<int> list(5);

未灭时间 发表于 2018-3-14 17:23:57

BngThea 发表于 2018-3-14 17:15
你没有写默认构造函数,只写了一个带一个参数的构造函数,实例化的时候需要传初值
LinearList list(5);

呃。。。

未灭时间 发表于 2018-3-14 17:25:15

其实这些我都试过,就是看不懂最后这个报错

人造人 发表于 2018-3-14 20:00:14

未灭时间 发表于 2018-3-14 17:25
其实这些我都试过,就是看不懂最后这个报错

//std::cerr << "存储分配错误!"; << std::endl;
std::cerr << "存储分配错误!" << std::endl;


#include<iostream>

template<class T>
class LinearList
{
public:
        LinearList(int size);
        ~LinearList();
        void GetElem(T * l, int i, T * e);
protected:
        T * data;
        int length;
        int maxsize;
};

template<class T>
LinearList<T>::LinearList(int size)
{
        maxsize = size;
        length = 0;
        data = new T;
        if(data == 0)
        {
                //std::cerr << "存储分配错误!"; << std::endl;
                std::cerr << "存储分配错误!" << std::endl;
                exit(1);
        }
}

template<class T>
LinearList<T>::~LinearList()
{
        delete[] data;
}
//template<class T>
// void LinearList<T>::GetElem(T *l, int i, T *e)
//{
//      *e = l;
//}


int main(void)
{
        LinearList<int> list(10);

        return 0;
}

未灭时间 发表于 2018-3-15 21:52:49

人造人 发表于 2018-3-14 20:00
//std::cerr

改了,还是一样的报错

人造人 发表于 2018-3-15 22:15:19

未灭时间 发表于 2018-3-15 21:52
改了,还是一样的报错

我需要错误信息

在我这边正常
$ ls
main.cpp
$ cat main.cpp
#include<iostream>

template<class T>
class LinearList
{
public:
      LinearList(int size);
      ~LinearList();
      void GetElem(T * l, int i, T * e);
protected:
      T * data;
      int length;
      int maxsize;
};

template<class T>
LinearList<T>::LinearList(int size)
{
      maxsize = size;
      length = 0;
      data = new T;
      if(data == 0)
      {
                //std::cerr << "存储分配错误!"; << std::endl;
                std::cerr << "存储分配错误!" << std::endl;
                exit(1);
      }
}

template<class T>
LinearList<T>::~LinearList()
{
      delete[] data;
}
//template<class T>
// void LinearList<T>::GetElem(T *l, int i, T *e)
//{
//      *e = l;
//}


int main(void)
{
      LinearList<int> list(10);

      return 0;
}

$ g++ main.cpp
$ ls
a.outmain.cpp
$

未灭时间 发表于 2018-3-15 23:01:25

人造人 发表于 2018-3-15 22:15
我需要错误信息

在我这边正常

人造人 发表于 2018-3-15 23:04:40

未灭时间 发表于 2018-3-15 23:01


这样看不出什么
我需要一个完整的截图
如果可以,截一下整个vs窗口的截图

未灭时间 发表于 2018-3-16 13:01:59

人造人 发表于 2018-3-15 23:04
这样看不出什么
我需要一个完整的截图
如果可以,截一下整个vs窗口的截图

人造人 发表于 2018-3-16 13:31:43

未灭时间 发表于 2018-3-16 13:01


好了,现在可以贴代码了,那三个文件都贴出来

未灭时间 发表于 2018-3-16 16:46:11

#pragma once
#include<iostream>
template<class T>
class LinearList
{
public:
        LinearList(int size);
        ~LinearList();
       
protected:
        T *data;
        int length;
        int maxsize;
};

未灭时间 发表于 2018-3-16 16:49:15


#include"list.h"
#include<iostream>
template<class T>
LinearList<T>::LinearList(int size)
{
        maxsize = size;
        length = 0;
        data = new T;
        if (data == 0)
        {
                std::cerr << "存储分配错误!" << std::endl;
                exit(1);
        }
}

template<class T>
LinearList<T>::~LinearList()
{
        delete[] data;
}

未灭时间 发表于 2018-3-16 16:49:50

人造人 发表于 2018-3-16 13:31
好了,现在可以贴代码了,那三个文件都贴出来

#include"list.h"
#include<iostream>

int main(void)
{
        LinearList<int> list(100);
       
        return 0;
}

人造人 发表于 2018-3-16 17:30:56

未灭时间 发表于 2018-3-16 16:49


简单来说,就是 声明和定义不能分开
刚刚看了前三篇文章,有兴趣的话,自己研究

未灭时间 发表于 2018-3-16 19:49:46

人造人 发表于 2018-3-16 17:30
简单来说,就是 声明和定义不能分开
刚刚看了前三篇文章,有兴趣的话,自己研究

谢谢,我去看一下
页: [1]
查看完整版本: 问一下c++模板类的调用问题