Archived

This topic is now archived and is closed to further replies.

jarod83

Arrays and Classes

Recommended Posts

OK... I have this code...and of course some errors:
  
class SomeClass
{
   public:
        void SomeFunction();
        ...
   private:
        char* match_active_results;
};

...

void SomeClass::SomeFunction()
{
     match_active_results = new char[rows][12];
     ...
}
  
What is wrong here? Is it possible to make this declaration?

Share this post


Link to post
Share on other sites
Yeah, the use of new is incorrect. What you want is something like this:


class SomeClass {
public:
void SomeFunction();
...
private:
char** match_active_results;
};

...

void
SomeClass::SomeFunction() {
match_active_results = new char*[rows];
for (int i = 0; i < rows; ++i) {
match_active_results = new char[col_size];
}
}


[edited by - aggregate on November 1, 2002 5:16:26 PM]

Share this post


Link to post
Share on other sites
new cannot create multidimensional arrays.
//////////// unidimensional array

  
char* match_active_results = new char[rows * 12];

match_active_results[i+12*j] = 'a';

delete[] match_active_results


//////////// or (C-style)

  
char** match_active_results = new char*[rows];
for( size_t i = 0; i < rows; ++i )
match_active_results[i] = new char[12];

match_active_results[i][j] = 'a';

for( size_t i = 0; i < rows; ++i )
delete[] match_active_results[i];
delete[] match_active_results

//////////// or (standard C++)

  
std::vector< std::vector<char> > match_active_results( rows, std::vector<char>( 12 ) );

match_active_results[i][j] = 'a';

// no destruction needed


//////////// or (boost)

  
boost::multi_array<char,2> match_active_results( boost::extents[rows][12] );

match_active_results[i][j] = 'a';

// no destruction needed


//////////// or ... many other solutions



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on November 1, 2002 5:16:27 PM]

Share this post


Link to post
Share on other sites