Thread: vectors and classes

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    vectors and classes

    Hey, I'm having some problems populating a two-dimensional vector that I defined in a class. I would like to do something like this:
    Code:
    class A {
       public:
         A();                    // constructor
         print_A();           // print function
      
       private:
         vector<vector<char> > a_vector;
    }; // A
    
    A::A() {
       a_vector.resize(8,8);      // re-size the vector to an 8x8 grid
    }
    This is just a brief example of what I'm trying to do, but when I try to compile my code, it gives me an error in the constructor: "sorry, not implemented: 'overload not supported by dump_expr'". I'm not sure if this is a problem with my compilier (bloodshed dev-c++ version 4, which was the last stable release), or just a quirk in the code.

    Also, I am planning on using several classes in my program that all use the same vector. Would pointers be the best way to access\change the data stored in the vector?

    Thanks.

    jim

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>a_vector.resize( 8, 8 );
    >>void resize( size_type size, TYPE val );
    This will NOT resize the vector to an 8x8 matrix. It will try to create 8 'vector< char >( 8 )'s, which will return an error. Instead, use this.

    Code:
    for( int i = 0; i < 8; i++ ) {
    	a_vector.push_back( vector< char >( 8, '\0' ) );
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    works!

    works great, thank you very much. Just a question though, what does the '\0' do? Does that just insert a null value to the cells?

    Cheers,

    Jim

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yes. The second parameter ('\0') is the value to initalise the chars to.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  2. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 04:19 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  4. Vectors + Classes = Confusing
    By Epo in forum C++ Programming
    Replies: 59
    Last Post: 12-18-2004, 04:42 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM