473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I use this typedef in a vector?

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<taxTabletreasury;

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 '07 #1
4 2874
Aa******@gmail.com 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<taxTabletreasury;

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<taxTabletreasury;

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

john
Jun 26 '07 #2
On 26 Jun, 07:45, Aalaa...@gmail.com 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<taxTabletreasury;
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

Jun 26 '07 #3
Aa******@gmail.com 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<taxTabletreasury;

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 '
Jun 26 '07 #4

<Aa******@gmail.comwrote 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<taxTabletreasury;
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<intTaxArr( tax, tax + Size );
std::vector<intTaxArr2( 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
Jun 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, I have included below a header file for a module I'm working on. Please note the line marked "***** ERROR HERE *****". At this line, the type dl_t is not being seen for some reason....
5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
5
by: lou zion | last post by:
hi all, i want to create a vector where each element is an array of 40 doubles. is this valid? something like: void abc( std::vector<double> InVals) { typedef double DataSeriesType;...
3
by: Eric Lilja | last post by:
Hello, got an odd question. I need a good typedef name for the type: std::vector<std::vector<std::pair<std::string, std::string> > > Even with using-statements to get rid of std:: it's simply too...
7
by: Gurikar | last post by:
Does typedef vector<std::string> vNames; creates vector object?
5
by: Russell Shaw | last post by:
Hi, In setjmp.h on a linux system, there is: typedef struct __jmp_buf_tag { ... } jmp_buf; I could understand typedef struct { } jmp_buf, but how do i interpret typedef struct { } jmp_buf ?
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
8
by: Allerdyce.John | last post by:
Is it a good practice in C++ to: 1. create a typedef for my STL container of my class? like this: typedef vector<A> AList; and I create my vector of A using this: AList* aList = new...
19
by: eiji | last post by:
Hi folks, I found this code in a project: > class TestList : public std::vector<Test> { /*empty!!!*/ }; Basicly this ends up in the same result like > typedef...
3
by: aaragon | last post by:
Hello everyone, I'm trying to run some simple code but for some reason it doesn't work and I've been staring at it for a long time without a single clue of what's going on. This is what happens,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.