1

コメント で回答された質問 私の評判のため、通常の方法で回答することはできません。後で回答に詳細を追加しますが、コメントで既に対処しています。ありがとう。* *

皆さんこんにちは -

質問に基づいて間違いなくわかるように、私は C++ は初めてですが、いくつかの高レベル言語の経験があります。(助けるよりも傷つけているようです)

クラスの場合、整数に型指定された配列のラッパーを作成する必要があります。(クラスのこの段階ではテンプレートはありません) また、クラスがゼロ以外の開始インデックスを持つことを許可する必要があります。クラスでメンバー配列を使用してデータを格納し (クラスのこの時点ではまだベクトルはありません)、パブリック メソッドから何らかの変換を行って、適切な内部配列要素にアクセスしています。

私が直面している問題は、コンパイル時に内部配列のサイズがわからないため、それをクラス グローバル ポインターとして宣言し、コンストラクターでサイズを設定していることです。コード スニペットは以下の問題領域にあります。

int *list;
safeArray::safeArray(int start, int initialSize)
{
    if(initialSize <= 0)
    {
        throw "Array size must be a positive integer";
    }
    maxSize = initialSize + 1;
    startIndex = start;
    endIndex = start + initialSize;
    list = new int[maxSize];    // Error thrown here
    int *tempArray = new int[maxSize];
    copyArray(tempArray);
    clearArray();   
}

私が得ているエラーは

Incompatible types in assignment of 'int*' to 'int[0u]'

int[0u] の型が何であるかは 100% わかりません。それはリテラル値ゼロで、u は符号なしですか? maxSize が値を保持していることをデバッガーで確認しました。また、それを定数の整数値に置き換えたところ、同じエラーが発生しました。

私のint *tempArray = new int[maxSize]; 行は機能していたので、宣言とサイズ変更を同時に行う必要があることに関係があるのではないかと考え、memcpy を実行することにしました。(これは実際には割り当ての範囲外であるため、他に何か不足しているに違いありません) memcpy は、他の変数を壊しているように見えるため失敗します。リストのアドレスをGDBに出力すると、コード内の別のグローバル変数と同じアドレスが返されるため、そのルートも割り当ての範囲外に見えました。

new私が他のフォーラムで見た共通のテーマは、他の変数のように配列を割り当てることができないということですが、それがステートメントを含むとは思いませんでした。私はその仮定で間違っていますか?

現在表示されているコンパイル エラーは上記のエラーのみlist = new int[maxSize];で、コード内のすべてのステートメントで表示されます。

私の質問は次のとおりです。

  1. int[0u] 型とは何ですか? また、その型はどこで生成されていますか? それは新しい声明からのものでなければなりませんよね?

  2. クラス内で動的配列リソースを利用する最良の方法は何ですか? ベクトルを使用する以外に?=)

関連する情報はこれですべてだと思いますが、重要なデータを見逃していたら申し訳ありません。以下は実装コードの残りの部分です。

/*
 *  safeArray.cpp
 *  safearray
 *
 *  Created by Jeffery Smith on 6/1/11.
 *  
 *
 */

#include "safeArray.h"
#include &lt;iostream&gt;


using namespace std;


    int startIndex = 0;
    int endIndex = 0;
    int maxSize = 1;
    int currentSize = 0;
    int *list;

safeArray::safeArray(int start, int initialSize)
{
    if(initialSize <= 0)
    {
        throw "Array size must be a positive integer";
    }
    maxSize = initialSize + 1;
    startIndex = start;
    endIndex = start + initialSize;
    list = new int[maxSize];    // Error thrown here
    int *tempArray = new int[initialSize + 1];
    copyArray(tempArray);
    clearArray();

}

safeArray::safeArray(const safeArray &sArray)
{
    list = new int[sArray.maxSize];
    copyArray(sArray);
    startIndex = sArray.startIndex;
    endIndex = sArray.endIndex;
    maxSize = sArray.maxSize;
    currentSize = sArray.currentSize;
}

void safeArray::operator=(const safeArray &right)
{
    list = new int[right.maxSize];
    copyArray(right);
    startIndex = right.startIndex;
    endIndex = right.endIndex;
    maxSize = right.maxSize;
    currentSize = right.currentSize;
}

safeArray::~safeArray()
{
    delete [] list;
}



int safeArray::operator[](int index)
{
    if(OutofBounds(index))
    {
        throw "You tried to access an element that is out of bounds";
    }
    return list[index - startIndex];
}

void safeArray::add(int value)
{
    if(this->isFull())
    {
        throw "Could not add element. The Array is full";
    }
    currentSize++;
    list[currentSize + startIndex];
}

void safeArray::removeAt(int value)
{
    if(OutofBounds(value))
    {
        throw "The requested element is not valid in this list";
    }
    compressList(value);
    currentSize--;
}

void safeArray::insertAt(int location, int value)
{
    if(OutofBounds(location) || this->isFull())
    {
        throw "The requested value is either out of bounds or the list is full";
    }
    expandList(location, value);
    currentSize++;
}


void safeArray::clearList()
{
    clearArray();
}

bool safeArray::isFull()
{
    return(maxSize == currentSize);
}

int safeArray::length()
{
    return currentSize;
}

int safeArray::maxLength()
{
    return this->maxSize;
}

bool safeArray::isEmpty()
{
    return(currentSize == 0);
}

bool safeArray::OutofBounds(int value)
{
    return (value > endIndex || value < startIndex);
}

void safeArray::clearArray()
{
    for(int i = 0; i < maxSize; i++)
    {
        list[i] = 0;
    }
    currentSize = 0;
}

void safeArray::compressList(int value)
{
    for(int i = value; i < endIndex; i++)
    {
        list[i] = list[i + 1];
    }
}

void safeArray::expandList(int location, int value)
{
    int tempHolder = list[location];
    list[location] = value;
    for(int i = location; i < endIndex; i++)
    {
        tempHolder = list[location];
        list[location] = value;
        value = tempHolder;
    }
}

void safeArray::copyArray(int *srcAddr )
{

    memcpy(list, srcAddr, sizeof(int) * maxSize);

}

void safeArray::copyArray(const safeArray &sArray)
{

    memcpy(list, &sArray, sizeof(int) * maxSize);

}

ヘッダーの定義は次のとおりです。


/*
 *  safeArray.h
 *  safearray
 *
 *  Created by Jeffery Smith on 6/1/11.
 *  Copyright 2011 Accenture. All rights reserved.
 *
 */



class safeArray {

public:
    safeArray(int,int);    //Standard constructor
    ~safeArray();          //Destructor
    int operator[](int);
    void operator=(const safeArray&);   //Assignment overload
    safeArray(const safeArray &sArray); //Copy Constructor

    void add(int);
    int maxLength();
    int length();
    bool isFull();
    bool isEmpty();
    void clearList();
    void removeAt(int);
    void insertAt(int,int);

protected:
    int list[];
    int startIndex;
    int endIndex;
    int maxSize;
    int currentSize;

private:
    void clearArray();
    bool OutofBounds(int);
    void expandList(int,int);
    void compressList(int);
    void copyArray(int*);
    void copyArray(const safeArray&);
};
4

2 に答える 2

0

@Boはコメントで私を助けてくれました。ヘッダー ファイルに古い int list[] 宣言があり、変更していないことがわかりました。そのため、スローされていたコンパイラ エラーは、そこでの宣言が原因でした。その後、すべてが肉汁でした。

于 2011-06-05T15:40:31.830 に答える
0

int[0u]? C では、可変サイズの構造体を効果的に使用できるように、構造体の末尾に長さ 0 の配列を配置できると思いますが、これは C++ では行われません。あなたのコードには、違法なコードとなるものは何もありません。ひどい、はい、違法、いいえ。のコンテンツを投稿する必要がありsafearray.hます。標準ヘッダーが含まれている場合は、 の使用がusing namespace std;問題の原因になる可能性があります。

また、グローバル変数も悪いです。クラス内にポインターを配置するだけです。何か非常に悪いことをしていない限り、基本的にグローバル変数を使用する必要はありません。特に、たとえば、変数のシャドーイング、名前の衝突、およびその他の大規模な問題にさらされるためです。ああ、できればstd::exceptionまたはから派生した例外クラスをスローする必要がありますstd::runtime_error。誰も捕まえようとしませんconst char*。名前空間を使用しないでstdください。問題が発生する可能性があります。コピー コンストラクターや代入演算子を呼び出すのではなく、memcpy を使用して要素をコピーしますか? また、代入演算子から始めて、いくつかの場所でメモリをリークしました。

template<typename T> class safe_array {
    char* list;
    std::size_t arrsize;
    void valid_or_throw(std::size_t index) {
        if (index <= arrsize) {
            throw std::runtime_error("Attempted to access outside the bounds of the array.");
    }
public:
    safe_array(std::size_t newsize) 
    : list(NULL) {
        size = arrsize;
        list = new char[arrsize];
        for(std::size_t i = 0; i < arrsize; i++) {
            new (&list[i * sizeof(T)]) T();
        }
    }
    safe_array(const safe_array& ref) 
    : list(NULL) {
        *this = ref;
    }
    safe_array& operator=(const safe_array& ref) {
        clear();
        arrsize = ref.size;
        list = new char[arrsize];
        for(std::size_t i = 0; i < arrsize; i++) {
            new (&list[i * sizeof(T)]) T(ref[i]);
        }        
    }
    T& operator[](std::size_t index) {
        valid_or_throw(index);
        return static_cast<T&>(list[index * sizeof(T)]);
    }
    const T& operator[](std::size_t index) {
        valid_or_throw(index);
        return static_cast<const T&>(list[index * sizeof(T)]);
    }
    void clear() {
        if (list == NULL)
            return;
        for(std::size_t i = 0; i < size; i++) {
            (*this)[i].~T();
        }
        delete[] list;
        list = NULL;
        arrsize = 0;
    }
    std::size_t size() {
        return arrsize;
    }
    bool empty() {
        return (list == NULL);
    }
    ~safe_array() {
        clear();
    }
};

私が作成した比較的簡単なサンプルクラスは、一般的な方向性を示すはずです。のすべての機能を提供するわけではなくvector、たとえば自動サイズ変更や容量バッファリング (およびその他のいくつかの欠点) も提供されません。

于 2011-06-04T13:08:43.197 に答える