2013-07-04 46 views
2

我的实验室任务是创建一个模板类来实例化不同数据类型的数组。然而,当我打电话或者我的“默认”或“新”的构造函数模板作为一种数据类型不是INT等,我收到以下错误:错误:无法在作业中将'char *'转换为'int *'(也用于'double *'到'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

为什么您的'ptr'成员声明为'int *'?它不应该是'T *'吗? –

回答

4

error: cannot convert ‘char*’ to ‘int*’ in assignment

此错误是抱怨这一行:

ptr = new T [size]; 

其中较早,ptr声明为:

int * ptr; 

ptr = new T分配不会,除非T工作是int当你构建你的Template 。若要解决此问题,请将ptr的定义更改为:

T * ptr;