"const char *" 类型的参数与 "char" 类型的参数不兼容
argument of type "const char *" is incompatible with parameter of type "char"
我正在尝试使用指针和模板在 C++ 中实现动态数组,以便我可以接受所有类型。该代码在 int
下运行良好,但在使用 char
时会出错。我在网上尝试了其他 SO 问题,但对我的场景一无所知。
代码:
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class dynamicIntArray
{
private:
T *arrPtr = new T[4]();
int filledIndex = -1;
int capacityIndex = 4;
public:
// Get the size of array
int size(void);
// Check if array is empty
bool isEmpty(void);
// Insert a data to array
bool insert(T n);
// Show the array
bool show(void);
};
template <typename T>
int dynamicIntArray<T>::size(void)
{
return capacityIndex + 1;
}
template <typename T>
bool dynamicIntArray<T>::insert(T n)
{
if (filledIndex < capacityIndex)
{
arrPtr[++filledIndex] = n;
return true;
}
else if (filledIndex == capacityIndex)
{
// Create new array of double size
capacityIndex *= 2;
T *newarrPtr = new T[capacityIndex]();
// Copy old array
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
// Add new data
newarrPtr[++filledIndex] = n;
arrPtr = newarrPtr;
return true;
}
else
{
cout << "ERROR";
}
return false;
}
template <typename T>
bool dynamicIntArray<T>::show(void)
{
cout << "Array elements are: ";
for (int i = 0; i <= filledIndex; i++)
{
cout << arrPtr[i] << " ";
}
cout << endl;
return true;
}
int main()
{
dynamicIntArray<char> myarray;
myarray.insert("A");
myarray.insert("Z");
myarray.insert("F");
myarray.insert("B");
myarray.insert("K");
myarray.insert("C");
cout << "Size of my array is: " << myarray.size() << endl;
myarray.show();
}
错误:
invalid conversion from ‘const char*’ to ‘char’
编辑:
我尝试使用 dynamicIntArray<string> myarray;
这次它给出了分段错误。
myarray.insert("A");
… "A"
是 const char[]
,但插入需要 char
。您可以将其更改为 myarray.insert('A')
对于初学者来说,代码有几个错误。
例如函数 size
returns 无效值。
template <typename T>
int dynamicIntArray<T>::size(void)
{
return capacityIndex + 1;
}
最初数组分配了 4 个元素
T *arrPtr = new T[4]();
int capacityIndex = 4;
但是函数 returns 5 ( capacityIndex + 1
).
在函数insert
中,访问了超出分配数组的内存。例如,当 filledIndex 等于 capacityIndex - 1
时,则在此 if 语句中
if (filledIndex < capacityIndex)
{
arrPtr[++filledIndex] = n;
return true;
}
值n
写在等于capacityIndex
的位置,而索引的有效范围是[0, capacityIndex)
.
在此代码段中
capacityIndex *= 2;
T *newarrPtr = new T[capacityIndex]();
// Copy old array
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
增加了数据成员capacityIndex
capacityIndex *= 2;
但是您在 for 循环中使用了这个新值
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
复制arrPtr
指向的数组中不存在的元素。
至于编译器错误则在这样的语句中
myarray.insert("A");
您正在使用字符串文字。例如,字符串文字 "A"
的类型 const char[2]
在参数表达式中被转换为类型 const char *
.
您正尝试将此指针分配给类型为 char
的对象,该对象是分配给元素类型为 char
的动态数组的对象。
你应该这样写
myarray.insert( 'A' );
也就是说,您必须使用类型为 char
的字符文字而不是字符串文字 - 类型模板参数的类型。
我正在尝试使用指针和模板在 C++ 中实现动态数组,以便我可以接受所有类型。该代码在 int
下运行良好,但在使用 char
时会出错。我在网上尝试了其他 SO 问题,但对我的场景一无所知。
代码:
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class dynamicIntArray
{
private:
T *arrPtr = new T[4]();
int filledIndex = -1;
int capacityIndex = 4;
public:
// Get the size of array
int size(void);
// Check if array is empty
bool isEmpty(void);
// Insert a data to array
bool insert(T n);
// Show the array
bool show(void);
};
template <typename T>
int dynamicIntArray<T>::size(void)
{
return capacityIndex + 1;
}
template <typename T>
bool dynamicIntArray<T>::insert(T n)
{
if (filledIndex < capacityIndex)
{
arrPtr[++filledIndex] = n;
return true;
}
else if (filledIndex == capacityIndex)
{
// Create new array of double size
capacityIndex *= 2;
T *newarrPtr = new T[capacityIndex]();
// Copy old array
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
// Add new data
newarrPtr[++filledIndex] = n;
arrPtr = newarrPtr;
return true;
}
else
{
cout << "ERROR";
}
return false;
}
template <typename T>
bool dynamicIntArray<T>::show(void)
{
cout << "Array elements are: ";
for (int i = 0; i <= filledIndex; i++)
{
cout << arrPtr[i] << " ";
}
cout << endl;
return true;
}
int main()
{
dynamicIntArray<char> myarray;
myarray.insert("A");
myarray.insert("Z");
myarray.insert("F");
myarray.insert("B");
myarray.insert("K");
myarray.insert("C");
cout << "Size of my array is: " << myarray.size() << endl;
myarray.show();
}
错误:
invalid conversion from ‘const char*’ to ‘char’
编辑:
我尝试使用 dynamicIntArray<string> myarray;
这次它给出了分段错误。
myarray.insert("A");
… "A"
是 const char[]
,但插入需要 char
。您可以将其更改为 myarray.insert('A')
对于初学者来说,代码有几个错误。
例如函数 size
returns 无效值。
template <typename T>
int dynamicIntArray<T>::size(void)
{
return capacityIndex + 1;
}
最初数组分配了 4 个元素
T *arrPtr = new T[4]();
int capacityIndex = 4;
但是函数 returns 5 ( capacityIndex + 1
).
在函数insert
中,访问了超出分配数组的内存。例如,当 filledIndex 等于 capacityIndex - 1
时,则在此 if 语句中
if (filledIndex < capacityIndex)
{
arrPtr[++filledIndex] = n;
return true;
}
值n
写在等于capacityIndex
的位置,而索引的有效范围是[0, capacityIndex)
.
在此代码段中
capacityIndex *= 2;
T *newarrPtr = new T[capacityIndex]();
// Copy old array
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
增加了数据成员capacityIndex
capacityIndex *= 2;
但是您在 for 循环中使用了这个新值
for (int i = 0; i < capacityIndex; i++)
{
newarrPtr[i] = arrPtr[i];
}
复制arrPtr
指向的数组中不存在的元素。
至于编译器错误则在这样的语句中
myarray.insert("A");
您正在使用字符串文字。例如,字符串文字 "A"
的类型 const char[2]
在参数表达式中被转换为类型 const char *
.
您正尝试将此指针分配给类型为 char
的对象,该对象是分配给元素类型为 char
的动态数组的对象。
你应该这样写
myarray.insert( 'A' );
也就是说,您必须使用类型为 char
的字符文字而不是字符串文字 - 类型模板参数的类型。