473,386 Members | 1,602 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,386 software developers and data experts.

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];
};
Aug 18 '05 #1
6 1503

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:%3*******************@newsb.telia.net...
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.
Aug 19 '05 #2
Tony Johansson wrote:
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
Aug 19 '05 #3
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.
}

Aug 19 '05 #4
On 19 Aug 2005 02:17:58 -0700
"kolari" <gi**********@gmail.com> wrote:

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
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
Aug 19 '05 #5
Moritz Beller wrote:
On 19 Aug 2005 02:17:58 -0700
"kolari" <gi**********@gmail.com> wrote:
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;

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

Aug 19 '05 #6
On Fri, 19 Aug 2005 13:38:50 +0200
Kyle <in*****@e.mail.com> wrote:
delete [] arr;


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
Aug 19 '05 #7

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

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
by: Stou Sandalski | last post by:
Hi, I have a python library created by wrapping the C++ library using Boost.Python, the problem is that the wrappers are not very pythonic.... so I want to add some methods that do not exist in...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.