473,326 Members | 2,108 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,326 software developers and data experts.

Problem initializing class with literal array!

Greetings.

I have a class that I would like to initialize with a literal array but it
is not accepted by the compiler. It works if I use a array variable but not
a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; //
OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}

Jul 22 '05 #1
3 5449

"Pedro Miguel Carvalho" <*S*********************@sapo.pt> wrote in message
news:41***********************@news.telepac.pt...
Greetings.

I have a class that I would like to initialize with a literal array but it is not accepted by the compiler. It works if I use a array variable but not a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; // OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}
There are no array literals in C++, { 0.0, 1.0 ... } is an initialiser that
can only be used in certain contexts, it is not an array literal.

It is not possible to have arrays as parameters in C++.
test( const float T[10] ) {
This code does not mean your constructor has an array for a parameter. The
compiler converts your code to this.

test( const float* T ) {

Both these reasons mean that your code cannot work.
test T1 = V;


This is the best you can do.

john
Jul 22 '05 #2
Pedro Miguel Carvalho posted:
Greetings.

I have a class that I would like to initialize with a literal array
but it is not accepted by the compiler. It works if I use a array
variable but not a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V;
//
OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error

....some-one's confused!

To achieve that syntax, you'd need something like:
struct test
{
float a;
float b;
float c;
float d;
...
};

test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

struct test
{
float blah[10];
};

I can see this discussion getting pretty tedious ( member variables,
aggregates, arrays decaying to pointers ) :-(
-JKop

-JKop
Jul 22 '05 #3
Pedro Miguel Carvalho wrote:
I have a class that I would like to initialize with a literal array but it
is not accepted by the compiler. It works if I use a array variable but not
a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
This is essentially

test(float const * T)

.. In function argument lists arrays decay into pointers.
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; //
OK
It's OK because 'V' (the name of the array) has also decayed into
a pointer to the first element, so what you have is

test T1(test(&V[0]));

which is fine because 'test's constructor expects a pointer to const
float, and &V[0] is a pointer to float.
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}


Well, what can I tell you? The first of the two syntaxes is reserved
for initialising _aggregates_. T2 is not an aggregate. That's why you
get an error in the first case. The second case presumes the existence
of some kind of "array literal". There are no array literals in C++.

So, you're stuck with the solution you've already found.

Victor
Jul 22 '05 #4

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

Similar topics

11
by: Randell D. | last post by:
Folks I have a multi-dimensional array in javascript, as follows: gameRecord=new Array(500); gameRecord="FA Cup : 19 February 2005"; gameRecord="Arsenal v Sheff Utd, 12:30";...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: Hendrik Schober | last post by:
Hi, I am trying to test a command line parser. So I came up with this (simplified): struct test { int argc; const char** argv; template< int N >
7
by: Neo Geshel | last post by:
Greetings. I have a serious problem. I have multiple sets of tables, several of which are chained more than two tables deep. That is, I have a parent, a child, and a great-grandchild table. ...
4
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have...
5
by: David Mathog | last post by:
I'm looking at a program which stores perl scripts in an array. Each script is stored as a single entry in that array, and the whole set of them live in a single header file (escaped out the wazoo...
2
raveendrabikkina
by: raveendrabikkina | last post by:
03/17 15:45:40 error Error creating form bean of class epost.form.BpUploadForm java.lang.InstantiationException: epost.form.BpUploadForm at java.lang.Class.newInstance0(Class.java:291) ...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.