Connecting Tech Pros Worldwide Help | Site Map

create an array by value

  #1  
Old August 19th, 2005, 12:55 AM
Tony Johansson
Guest
 
Posts: n/a
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

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

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

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

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

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

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do you pass an array by value Abhi answers 14 March 28th, 2007 10:35 PM
Returning an array by reference from a function Krackers answers 6 June 28th, 2006 04:02 AM
create an array of field to test if they are null SAN CAZIANO answers 1 July 23rd, 2005 03:29 PM
Can I create an array of tags by assigning same name? Jenny answers 1 July 23rd, 2005 03:25 PM