Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 19th, 2005, 12:55 AM
Tony Johansson
Guest
 
Posts: n/a
Default create an array by value

Hello experts

Why is not possible to compile the code for class FloatArray below.
I get error the following compile error
c:\Documents and Settings\Tony\kau\cplusplus\test10\Boolean.h(90): error
C2065: 'length' : undeclared identifier

If a instead use the FloatArray as a class template and add
this line template <int size> just before the class definition.
Then I get no errors.


class FloatArray
{
public:
FloatArray(int size) : length(size)
{}

private:
float array[length];
};


  #2  
Old August 19th, 2005, 01:15 AM
Dave
Guest
 
Posts: n/a
Default Re: create an array by value


"Tony Johansson" <johansson.andersson@telia.com> wrote in message
news:%39Ne.31850$d5.185510@newsb.telia.net...[color=blue]
> Hello experts
>
> Why is not possible to compile the code for class FloatArray below.
> I get error the following compile error
> c:\Documents and Settings\Tony\kau\cplusplus\test10\Boolean.h(90): error
> C2065: 'length' : undeclared identifier
>
> If a instead use the FloatArray as a class template and add
> this line template <int size> just before the class definition.
> Then I get no errors.
>
>
> class FloatArray
> {
> public:
> FloatArray(int size) : length(size)
> {}
>
> private:
> float array[length];
> };
>
>[/color]

Can you show me where you declare the variable named length that you are
initializing? There is none! You're initializing a non-existent member!

As far as the template change... Unless you instantiate the template, the
code only gets syntax checked - no semantic checks (such as type checks) are
done. That's why it appeared to compile OK.


  #3  
Old August 19th, 2005, 01:35 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: create an array by value

Tony Johansson wrote:[color=blue]
> Why is not possible to compile the code for class FloatArray below.[/color]

Because your array declaration _must_ have the size as a compile-time
constant expression. 'length' doesn't seem to be a constant expression.
[color=blue]
> I get error the following compile error
> c:\Documents and Settings\Tony\kau\cplusplus\test10\Boolean.h(90):
> error C2065: 'length' : undeclared identifier
>
> If a instead use the FloatArray as a class template and add
> this line template <int size> just before the class definition.
> Then I get no errors.[/color]

Have you tried instantiating that template yet?
[color=blue]
> class FloatArray
> {
> public:
> FloatArray(int size) : length(size)
> {}
>
> private:
> float array[length];
> };[/color]

Why don't you simply use 'vector<float>'? Or 'vector<double>', for
that matter... (always prefer 'double' over 'float' unless you have
a strong enough reason to use 'float')

V


  #4  
Old August 19th, 2005, 10:25 AM
kolari
Guest
 
Posts: n/a
Default Re: create an array by value

In the abow program the array size is not specified at compile time,
which was your error .

If You really want to do that then create a pointer of flot type and
allocate memory at run time.

....
private:
float *arr;
public:
FloatArray(int size)
{
arr= new [size]; //I am not remembering the syntax
}
.....
Then you can allocate memory at run time.
}

  #5  
Old August 19th, 2005, 12:35 PM
Moritz Beller
Guest
 
Posts: n/a
Default Re: create an array by value

On 19 Aug 2005 02:17:58 -0700
"kolari" <girishkolari@gmail.com> wrote:

[color=blue]
> arr= new [size]; //I am not remembering the syntax[/color]

arr = new float[size];
!

Don't forget to add an appropriate destructor then:
~FloatArray() {
delete arr;
}

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
  #6  
Old August 19th, 2005, 12:45 PM
Kyle
Guest
 
Posts: n/a
Default Re: create an array by value

Moritz Beller wrote:[color=blue]
> On 19 Aug 2005 02:17:58 -0700
> "kolari" <girishkolari@gmail.com> wrote:
>
>
>[color=green]
>> arr= new [size]; //I am not remembering the syntax[/color]
>
>
> arr = new float[size];
> !
>
> Don't forget to add an appropriate destructor then:
> ~FloatArray() {
> delete arr;[/color]
delete [] arr;[color=blue]
> }
>
> best regards / Gruß
> Moritz Beller
> --
> web http://www.4momo.de
> mail momo dot beller at t-online dot de
> gpgkey http://gpg.notlong.com[/color]
  #7  
Old August 19th, 2005, 07:15 PM
Moritz Beller
Guest
 
Posts: n/a
Default Re: create an array by value

On Fri, 19 Aug 2005 13:38:50 +0200
Kyle <invalid@e.mail.com> wrote:
[color=blue]
> delete [] arr;[/color]

Right.

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles