错误:无法在赋值时将'char *'转换为'int *'(也用于'double *'到'int *')

时间:2013-07-04 06:21:01

标签: c++ templates

我的实验室任务是创建一个模板类,用于实例化不同数据类型的数组。但是,当我打电话给我的默认'或者' new'模板的构造函数作为INT以外的数据类型,我收到以下错误:

Template.h: In constructor ‘Template<T>::Template() [with T = char]’:
main.cc:52:20:   instantiated from here
Template.h:71:5: error: cannot convert ‘char*’ to ‘int*’ in assignment
Template.h: In constructor ‘Template<T>::Template(int) [with T = char]’:
main.cc:58:35:   instantiated from here
Template.h:89:5: error: cannot convert ‘char*’ to ‘int*’ in assignment
Template.h: In constructor ‘Template<T>::Template(int) [with T = double]’:
main.cc:72:36:   instantiated from here
Template.h:89:5: error: cannot convert ‘double*’ to ‘int*’ in assignment
Template.h: In copy constructor ‘Template<T>::Template(const Template<T>&) [with T = double]’:
main.cc:73:35:   instantiated from here
Template.h:101:5: error: cannot convert ‘double*’ to ‘int*’ in assignment

头文件:(在构造函数定义后截断,如有必要,将发布以回答我的问题)

#ifndef TEMPLATE_H
#define TEMPLATE_H
#include <iostream>    
using namespace std;

template <typename T>
class Template {
    friend istream & operator>>( istream & in, Template<T> & a ) {
       a.input();
       return in;
    }
    friend ostream & operator<<( ostream & out, const Template<T> & a ) {
       a.output();
       return out;
    }
public:
    Template<T> ();   // Default constructor
    Template<T> ( int );   // New constructor
    Template<T> ( const Template<T> & );   // Copy constructor
    ~Template<T> ();   // Destructor.

    // Operator overload prototypes.
    const Template<T> & operator=( const Template<T> & );
    const bool operator==( const Template<T> & );
    const bool operator!=( const Template<T> & );

    T & operator[]( int );  // Left hand operator.
    T operator[]( int ) const;  // Right hand operator.
private:
    // Member function prototypes
    bool isEqual ( const Template<T> & );
    void input();
    void output() const;

    static const int DEFAULTSIZE = 10; // Set default array size.
    static const int MAXSIZE = 100; // Set maximum array size.
    static T DEFAULTVALUE; // Set value of array elements.
    int size;
    int * ptr;
};    #endif

template <typename T>
T Template<T>::DEFAULTVALUE; // Instantiate DEFAULTVALUE of ADT T

/****************************Constructors*****************************/

// Default constructor
template <typename T>
Template<T>::Template() {
    size = DEFAULTSIZE;
    ptr = new T [size];     // Allocate memory for array[DEFAULTSIZE].
    for (int i = 0; i < size; i++) 
        ptr[i] = DEFAULTVALUE;  // Set all elements to DEFAULTVALUE.
}

// New constructor
template <typename T>
Template<T>::Template( int a ) {
    if (a < 1)
        size = DEFAULTSIZE; // Can't allocate array of <= 0 elements.
    else if (a > MAXSIZE)
        size = MAXSIZE; // Template can be no larger than 100 elements.
    else
        size = a;   // Valid argument, passed to size.

    ptr = new T [size];
    for (int i = 0; i < size; i++) 
        ptr[i] = DEFAULTVALUE;  // Set all elements to DEFAULTVALUE.
}

// Copy constructor
template <typename T>
Template<T>::Template( const Template<T> & b ) {
    size = b.size;
    ptr = new T [size]; // Set size to argument's array size.
    for (int i = 0; i < size; i++)
        ptr[i] = b.ptr[i]; // Copy elements.
}

// Destructor
template <typename T>
Template<T>::~Template () {
    for (int i = 0; i < size; i++) {
        delete ptr; // Deallocate memory.
        ptr = NULL; // Delete array elements.
    }
}

main.cc文件的相关部分:(从第52行开始)

    Template<char> charOne; // Default constructor called for charOne

    cout << "Now specify size of array charOne: ";
    cin >> arrSize;
    cout << endl;

    Template<char> charTwo(arrSize); // New constructor call

    // Print contents of both arrays.
    cout << "Printing contents of both arrays:\n" << "charOne: " 
         << charOne << "charTwo: " << charTwo;

    // Check equality of charOne & charTwo.
    if (charOne == charTwo)
        cout << "charOne == charTwo.\n" << endl;
    else 
        cout << "charOne != charTwo.\n" << endl;

/**************************Double Template***************************/

    Template<double> dblOne(arrSize);
    Template<double> dblTwo(dblOne);

1 个答案:

答案 0 :(得分:4)

  

错误:无法在作业中将'char *'转换为'int *'

此错误正在抱怨此行:

 ptr = new T [size];

之前,ptr被声明为:

int * ptr;

除非ptr = new T T int在您构建Template时,ptr分配才会有效。要解决此问题,请将T * ptr; 的定义更改为:

{{1}}