Connecting Tech Pros Worldwide Forums | Help | Site Map

create an array by value

Tony Johansson
Guest
 
Posts: n/a
#1: Aug 19 '05
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];
};



Dave
Guest
 
Posts: n/a
#2: Aug 19 '05

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.


Victor Bazarov
Guest
 
Posts: n/a
#3: Aug 19 '05

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


kolari
Guest
 
Posts: n/a
#4: Aug 19 '05

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.
}

Moritz Beller
Guest
 
Posts: n/a
#5: Aug 19 '05

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
Kyle
Guest
 
Posts: n/a
#6: Aug 19 '05

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]
Moritz Beller
Guest
 
Posts: n/a
#7: Aug 19 '05

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