How do I use this typedef in a vector?

Discussion in 'C++' started by AalaarDB@gmail.com, Jun 26, 2007.

  1. Guest

    Basically, how do I do this? If there is no legal way to do what I
    want, why not? It seems very logical.
    I want the array as a typedef for elegance and so I can do
    treasury.front()[0] or treasury[0][0]


    typedef int taxTable[8];//the type taxTable should be 8 ints
    int size = sizeof(taxTable);//32, shows the array is working ok
    taxTable taxes;
    std::vector<taxTable> treasury;

    treasury.push_back(taxes);
    //error C2440: 'initializing' : cannot convert
    //from 'const int [8]' to 'taxTable '

    treasury.resize(1);
    //error C2440: '<function-style-cast>' :
    //cannot convert from 'int' to 'taxTable '
    , Jun 26, 2007
    #1
    1. Advertising

  2. wrote:
    > Basically, how do I do this? If there is no legal way to do what I
    > want, why not? It seems very logical.
    > I want the array as a typedef for elegance and so I can do
    > treasury.front()[0] or treasury[0][0]
    >
    >
    > typedef int taxTable[8];//the type taxTable should be 8 ints
    > int size = sizeof(taxTable);//32, shows the array is working ok
    > taxTable taxes;
    > std::vector<taxTable> treasury;
    >
    > treasury.push_back(taxes);
    > //error C2440: 'initializing' : cannot convert
    > //from 'const int [8]' to 'taxTable '
    >
    > treasury.resize(1);
    > //error C2440: '<function-style-cast>' :
    > //cannot convert from 'int' to 'taxTable '
    >


    It's logical but unfortunately arrays are not logical in C/C++, never
    have been. For instance you cannot have any array type as a parameter in
    C/C++.

    You can do close to what you want however

    struct taxTable
    {
    int& operator[](size_t i) { return data; }
    int operator[](size_t i) const { return data; }
    int data[8];
    };

    std::vector<taxTable> treasury;

    Add whatever other methods you need to taxTable and you'll get pretty close.

    john
    John Harrison, Jun 26, 2007
    #2
    1. Advertising

  3. dasjotre Guest

    On 26 Jun, 07:45, wrote:
    > Basically, how do I do this? If there is no legal way to do what I
    > want, why not? It seems very logical.
    > I want the array as a typedef for elegance and so I can do
    > treasury.front()[0] or treasury[0][0]
    >
    > typedef int taxTable[8];//the type taxTable should be 8 ints
    > int size = sizeof(taxTable);//32, shows the array is working ok
    > taxTable taxes;
    > std::vector<taxTable> treasury;


    it is not legal, arrays are neither copyable nor
    assignable, two basic requirements for vector
    value type

    > treasury.push_back(taxes);
    > //error C2440: 'initializing' : cannot convert
    > //from 'const int [8]' to 'taxTable '


    yes, can not assign arrays.

    > treasury.resize(1);
    > //error C2440: '<function-style-cast>' :
    > //cannot convert from 'int' to 'taxTable '


    look at implementation of resize(size_type)
    it calls resize(size_type, value_type)

    regards

    DS
    dasjotre, Jun 26, 2007
    #3
  4. Fei Liu Guest

    wrote:
    > Basically, how do I do this? If there is no legal way to do what I
    > want, why not? It seems very logical.
    > I want the array as a typedef for elegance and so I can do
    > treasury.front()[0] or treasury[0][0]
    >
    >
    > typedef int taxTable[8];//the type taxTable should be 8 ints
    > int size = sizeof(taxTable);//32, shows the array is working ok
    > taxTable taxes;
    > std::vector<taxTable> treasury;
    >
    > treasury.push_back(taxes);
    > //error C2440: 'initializing' : cannot convert
    > //from 'const int [8]' to 'taxTable '


    Array cannot be copied directly. There is no copy construction of array
    data structure.
    >
    > treasury.resize(1);
    > //error C2440: '<function-style-cast>' :
    > //cannot convert from 'int' to 'taxTable '
    >
    Fei Liu, Jun 26, 2007
    #4
  5. BobR Guest

    <> wrote in message...
    > Basically, how do I do this? If there is no legal way to do what I
    > want, why not? It seems very logical.
    > I want the array as a typedef for elegance and so I can do
    > treasury.front()[0] or treasury[0][0]
    >
    > typedef int taxTable[8];//the type taxTable should be 8 ints
    > int size = sizeof(taxTable);//32, shows the array is working ok
    > taxTable taxes;
    > std::vector<taxTable> treasury;
    > treasury.push_back(taxes);
    > file://error C2440: 'initializing' : cannot convert
    > file://from 'const int [8]' to 'taxTable '
    > treasury.resize(1);
    > file://error C2440: '<function-style-cast>' :
    > file://cannot convert from 'int' to 'taxTable '


    Why not just use 'vector'?:

    std::size_t const Size( 7 );
    {
    int tax[ Size ] = { 15, 19, 3, 54, 12, 53, 25 };
    int tax2[ Size ] = { 5, 71, 53, 4, 112, 86, 2 };
    int tax3[ Size ] = { 52, 27, 35, 44, 2, 18, 3 };

    std::vector<int> TaxArr( tax, tax + Size );
    std::vector<int> TaxArr2( tax2, tax2 + Size );

    std::vector<std::vector<int> > TaxTables;
    TaxTables.push_back( TaxArr );
    TaxTables.push_back( TaxArr2 );
    TaxTables.push_back( std::vector<int>( tax3, tax3 + Size ) );

    for( std::size_t i(0); i < TaxTables.size(); ++i ){
    for( std::size_t k(0); k < TaxTables.at(i).size(); ++k ){
    std::cout<< TaxTables.at( i ).at( k ) <<" ";
    } // for(k)
    std::cout<<std::endl;
    } // for(i)
    }
    /* -output-
    15 19 3 54 12 53 25
    5 71 53 4 112 86 2
    52 27 35 44 2 18 3
    */

    --
    Bob R
    POVrookie
    BobR, Jun 27, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,769
  2. Replies:
    8
    Views:
    1,912
    Csaba
    Feb 18, 2006
  3. aalaardb

    How do I use this typedef in a vector?

    aalaardb, Jun 25, 2007, in forum: C Programming
    Replies:
    0
    Views:
    281
    aalaardb
    Jun 25, 2007
  4. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  5. oor
    Replies:
    0
    Views:
    1,343
Loading...

Share This Page