我的实验室任务是创建一个模板类来实例化不同数据类型的数组。但是,当我将模板的“默认”或“新”构造函数作为 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);