0

I'm trying to store a pointer in an array.

My pointer to a pointer is class object is:

classType **ClassObject;

So i know i can allocate it by using the new operator like this:

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

I'm reading a text file, with punctuation and here is what i have so far:

// 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.

I guess my questions are:

  • Have I allocated space for each word in the file?
  • How would i store a pointer in the ClassObject array within my while/for loop?
lc.
  • 113,939
  • 20
  • 158
  • 187
user40120
  • 648
  • 5
  • 28
  • 58
  • you need to read http://stackoverflow.com/editing-help and reformat your question to be more readable. – lothar Apr 12 '09 at 02:33

3 Answers3

3

If you are using C++ use the Boost Multidimensional Array Library

lothar
  • 19,853
  • 5
  • 45
  • 59
  • And you should look at this question too : http://stackoverflow.com/questions/365782/how-do-i-best-handle-dynamic-multi-dimensional-arrays-in-c-c/365800#365800 – Klaim Apr 12 '09 at 12:06
1

Hmm, I'm not sure what you want to do (especially new *classType[x] -- does this even compile?)

If you want a new classType for every word, then you can just go

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

provided that ClassObject is initialized (as you said).

You say you want a 2D array - if you want to do that, then the syntax is:

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

However, I'm also not sure of what you mean by new *classType[ 100 ] = {}; - what are the curly braces doing there? It seems like it should be

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

I highly suggest you use something else, though, as this is really nasty (and you have to take care of deletion... ugh)

Use vector<>s or as the above poster suggested, the boost libraries.

v3.
  • 2,003
  • 15
  • 18
0

Your code is perfectly fine excepting one line: ClassObject[x] = new *classType[x]; The star * needs to go away, and what you're probably trying to say is that you want ClassObject to be indexed to word count rather than x.

Replace that line with: ClassObject[wrdCount] = new classType[x];

Hope that helps, Billy3

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Im trying to pass the 1 word to the constructor, which takes a const character pointer paramater. I can call every function except for that one. Having ClassObject[][].WhatIneed( cArray ); wont work ClassObject[][].WhatIdontNeed(); works – user40120 Apr 12 '09 at 03:24