gpt4 book ai didi

c++ - 错误 : cannot convert ‘char*’ to ‘int*’ in assignment (also for 'double*' to 'int*' )

转载 作者:搜寻专家 更新时间:2023-10-31 01:08:58 25 4
gpt4 key购买 nike

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

最佳答案

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

此错误是关于这一行的:

 ptr = new T [size];

在前面,ptr 声明为:

int * ptr;

ptr = new T 赋值将不起作用,除非 Tint,当您构建您的 Template。要解决此问题,请将 ptr 的定义更改为:

T * ptr;

关于c++ - 错误 : cannot convert ‘char*’ to ‘int*’ in assignment (also for 'double*' to 'int*' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17463164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com