簡體   English   中英

“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; 這次它給出了分段錯誤。

對於初學者來說,代碼有幾個錯誤。

例如,函數size返回一個無效值。

template <typename T> 
int dynamicIntArray<T>::size(void)
{
return capacityIndex + 1;
}

最初為數組分配了 4 個元素

T *arrPtr = new T[4]();
int capacityIndex = 4;

但該函數返回 5 ( capacityIndex + 1 )。

在函數insert ,可以訪問超出分配數組的內存。 例如,當 fillIndex 等於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類型的字符文字 - 類型模板參數的類型。

myarray.insert("A"); "A"是一個const char[] ,但 insert 需要char 您可以將其更改為myarray.insert('A')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM