简体   繁体   English

使用模板C ++的输出流不起作用

[英]Output Stream with templates c++ doesn't work

I have a template class and i want to overload the output operator <<, but my code doesn't work, I don't know why... I don't know how do the same thing, but with the input operator... Can you help me? 我有一个模板类,我想重载输出运算符<<,但是我的代码不起作用,我不知道为什么...我不知道如何做同样的事情,但是使用输入运算符。 .. 你能帮助我吗?

This is my code: 这是我的代码:

#include <iostream>

template<class TypeData, int S>
class TD{
  private:
      int size;
      int augmentation;
      int capacity;

      TypeData * array;

      void changeCapacity();

  public:
    TD();

    TD(const TD<TypeData,S> & array);

    TypeData & operator [](int position) const;

    void addElement(TypeData element);

    friend std::ostream & operator << (std::ostream & output, const TD<TypeData, S> & array);

    ~TD();
};

template<class TypeData, int S>
TD<TypeData, S>::TD(): 
size(0), augmentation(S), capacity(S){
    this->array = new TypeData[S];
}

template<class TypeData, int S>
TD<TypeData, S>::TD(const TD<TypeData,S> & array): size (array.size),
augmentation(array.augmentation), capacity(array.capacity){
    this->array = new TypeData[array.capacity];

    for (int i = 0; i < array.size; i++){
        this->array[i] = array[i];
    }
}

template<class TypeData, int S>
TypeData& TD<TypeData, S>::operator [](int position) const{
    if (position > this->size){
            return this->array[0];
    }

    return this->array[position];
}

template<class TypeData, int S>
void TD<TypeData, S>::addElement(TypeData element){
    if (this->capacity <= this->size){
        TypeData * ptTmp = new TypeData[this->capacity];

        for (int i = 0; i < this->size; i++){
            ptTmp[i] = this->array[i];
        }

        delete this->array;

        this->capacity += this->augmentation;
        this->array = new TypeData[this->capacity];

        for (int i = 0; i < this->size; i++){
            this->array[i] = ptTmp[i];
        }

        delete[] ptTmp;
    }

    this->array[size] = element;
    size++;
}

template<class TypeData, int S>
std::ostream & operator << (std::ostream & output, const TD<TypeData, S> & array){
    for (int i = 0; i < array.size; i++){
        output << array[i] << " - ";
    }

    return output;
}

template<class TypeData, int S>
TD<TypeData, S>::~TD(){
    delete [] this->array;
}

Here the main: 这里主要:

int main(){

   TD<int,20> t;
   t.addElement(5);
   t.addElement(10);

   std::cout << t << std::endl;

   return 0;
}

I have this error: 我有这个错误:

Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&,  TD<int, 20> const&)", referenced from:
      _main in test9-8bd618.o
ld: symbol(s) not found for architecture x86_64

Here's one way to declare the operator<< function to be a friend of the class. 这是一种将operator<<函数声明为该类的friend的方法。

// Forward declare the class.
template<class TypeData, int S> class TD;

// Declare the function.
template<class TypeData, int S> 
std::ostream & operator << (std::ostream & output, const TD<TypeData, S> & array);

// Define the class.
template<class TypeData, int S>
class TD{
  private:
      int size;
      int augmentation;
      int capacity;

      TypeData * array;

      void changeCapacity();

  public:
    TD();

    TD(const TD<TypeData,S> & array);

    TypeData & operator [](int position) const;

    void addElement(TypeData element);

    // Declare the friend.
    // This makes sure that operator<< <int, 5> is a friend of TD<int, 5>
    // but not a friend of TD<double, 20>
    friend std::ostream & operator << <TypeData, S> (std::ostream & output, const TD<TypeData, S> & array);

    ~TD();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM