create an array by value

T

Tony Johansson

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];
};
 
D

Dave

Tony Johansson said:
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];
};

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

Victor Bazarov

Tony said:
Why is not possible to compile the code for class FloatArray below.

Because your array declaration _must_ have the size as a compile-time
constant expression. 'length' doesn't seem to be a constant expression.
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.

Have you tried instantiating that template yet?
class FloatArray
{
public:
FloatArray(int size) : length(size)
{}

private:
float array[length];
};

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
 
K

kolari

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

Moritz Beller

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

arr = new float[size];
!

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

best regards / Gruß
Moritz Beller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top