How do I use this typedef in a vector?

A

AalaarDB

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 '
 
J

John Harrison

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
 
D

dasjotre

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
 
F

Fei Liu

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

BobR

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top