0

ポインタを配列に格納しようとしています。

ポインタへの私のポインタはクラスオブジェクトです:

classType **ClassObject;

したがって、次のような新しい演算子を使用して割り当てることができます。

ClassObject = new *classType[ 100 ] = {};

句読点付きのテキストファイルを読んでいます。これまでのところ、次のようになっています。

// included libraries
// main function
// defined varaibles

classType **ClassObject; // global object
const int NELEMENTS = 100; // global index


wrdCount = 1;  // start this at 1 for the 1st word
while ( !inFile.eof() )  
{
   getline( inFile, str, '\n' );  // read all data into a string varaible
   str = removePunct(str);  // User Defined Function to remove all punctuation.
   for ( unsigned x = 0; x < str.length(); x++ )
   {
       if ( str[x] == ' ' ) 
       {
          wrdCount++;  // Incrementing at each space
          ClassObject[x] = new *classType[x];
       // What i want to do here is allocate space for each word read from the file.

       }
   }
}
// this function just replaces all punctionation with a space
string removePunct(string &str) 
{ 
    for ( unsigned x = 0; x < str.length(); x++ )
        if ( ispunct( str[x] ) )
            str[x] = ' ';
  return str;
}

// Thats about it.

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

  • ファイル内の各単語にスペースを割り当てましたか?
  • while / forループ内のClassObject配列にポインタを格納するにはどうすればよいですか?
4

3 に答える 3

3

C ++を使用している場合は、Boost多次元配列ライブラリを使用してください

于 2009-04-12T02:34:40.663 に答える
1

うーん、あなたが何をしたいのかわかりません (特に new *classType[x] -- これはコンパイルできますか?)

すべての単語に新しい classType が必要な場合は、そのまま移動できます

ClassObject[x] = new classType; //create a _single_ classType
ClassObject[x]->doSomething();

ClassObject が初期化されている場合(あなたが言ったように)。

あなたは2D配列が欲しいと言います-それをしたい場合、構文は次のとおりです。

ClassObject[x] = new classType[y]; //create an array of classType of size y
ClassObject[0][0].doSomething(); //note that [] dereferences automatically

ただし、 new *classType[ 100 ] = {}; の意味もわかりません。- 中括弧は何をしているのですか? そうあるべきらしい

classType** classObject = new classType*[100];

ただし、これは本当に厄介なので、別のものを使用することを強くお勧めします(そして、削除の世話をする必要があります...うーん)

vector<> を使用するか、上記のポスターが示唆するようにブースト ライブラリを使用します。

于 2009-04-12T03:08:02.020 に答える
0

あなたのコードは 1 行を除いて完全に問題ありません: ClassObject[x] = new *classType[x]; アスタリスク * を削除する必要があります。おそらく、あなたが言おうとしているのは、ClassObject をx ではなく単語数にインデックス付けすることです。

その行を次のように置き換えます。 ClassObject[wrdCount] = new classType[x];

お役に立てば幸いです、Billy3

于 2009-04-12T03:15:27.587 に答える