Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I use this typedef in a vector?

Reply
Thread Tools

How do I use this typedef in a vector?

 
 
AalaarDB@gmail.com
Guest
Posts: n/a
 
      06-26-2007
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 '

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i]; }
int operator[](size_t i) const { return data[i]; }
int data[8];
};

std::vector<taxTable> treasury;

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

john
 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      06-26-2007
On 26 Jun, 07:45, (E-Mail Removed) 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

 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      06-26-2007
(E-Mail Removed) 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 '
>

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      06-27-2007

<(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
How to use typedef in nesting class? =?gb2312?B?w7fR08zO?= C++ 2 06-10-2007 11:02 AM
Can I use typedef to define types used in the return type in template function? PengYu.UT@gmail.com C++ 2 11-07-2005 03:01 PM
is it any good to use typedef ? dreamcatcher C Programming 14 09-23-2003 01:27 PM



Advertisments