2013-07-04 3 views
2

Mon travail de laboratoire consiste à créer une classe modèle pour instancier des tableaux de différents types de données. Cependant, quand je l'appelle l'autre mon « défaut » ou « nouveau » constructeur pour le modèle comme un autre type de données que INT, je reçois l'erreur suivante:erreur: impossible de convertir 'char *' en 'int *' dans affectation (également pour 'double *' en '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 

fichier d'en-tête: (tronquée après les définitions du constructeur, affichera si nécessaire pour répondre à ma question)

#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. 
    } 
} 

partie pertinente du fichier main.cc: (en commençant à la ligne 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

Pourquoi votre membre 'ptr' est-il déclaré comme étant' int * '? Ne devrait-il pas être «T *»? –

Répondre

4

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

Cette erreur se plaint de cette ligne:

ptr = new T [size]; 

Si auparavant, ptr est déclarée comme:

int * ptr; 

L'affectation ptr = new T ne fonctionnera pas à moins T est int quand vous construisez votre Template . Pour résoudre ce problème, modifiez la définition de ptr à:

T * ptr; 
Questions connexes