473,386 Members | 1,997 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.

Initialising array and passing a constructor argument?

Hi,
I'm sure this is a relatively simple question but I can't work out the syntax for what I want to do. I have a class whose constructor takes a single argument. If I declare an array of this class, I still want to pass the argument to the constructor but I can't seem to arrange the square brackets (number of items) and the curly brackets (constructor argument) and get it to compile. Can this be done in one line? How can it be done?

Example:

Expand|Select|Wrap|Line Numbers
  1. myClass singleInstance=new myClass(arg); // triggering the constructor fine
  2.  
  3. myClass[] x=new myClass[10]; //can't I put the (arg) here?
Have tried a few ways but am stumped and can't find an answer easily on the web.

Cheers, Tom
Jan 24 '07 #1
3 4925
r035198x
13,262 8TB
Hi,
I'm sure this is a relatively simple question but I can't work out the syntax for what I want to do. I have a class whose constructor takes a single argument. If I declare an array of this class, I still want to pass the argument to the constructor but I can't seem to arrange the square brackets (number of items) and the curly brackets (constructor argument) and get it to compile. Can this be done in one line? How can it be done?

Example:

Expand|Select|Wrap|Line Numbers
  1. myClass singleInstance=new myClass(arg); // triggering the constructor fine
  2.  
  3. myClass[] x=new myClass[10]; //can't I put the (arg) here?
Have tried a few ways but am stumped and can't find an answer easily on the web.

Cheers, Tom
You are creating an array of myClasses do you want to initialize them all with the same value? If so then
Expand|Select|Wrap|Line Numbers
  1. myClass[] x=new myClass[10]; 
  2. for(int i = 0; i < x.length;i++) {
  3.    myClass[i] = new myClass(arg);
  4. }
is the proper way of initialising the array
Jan 24 '07 #2
Thanks for the reply.

I get this error with your code.
myEg.java:20: cannot find symbol
symbol : variable myClass
location: class myEg
myClass[i] = new myClass(arg)
;
myClass is the name of the class. I assume you meant:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < x.length;i++) {
  2.    x[i] = new myClass(arg);
  3. }
which compiles fine.

And I assume that even if I wanted to pass different arguments I could do so by continuing:

Expand|Select|Wrap|Line Numbers
  1. x[1] = new myClass(arg);
  2. x[2] = new myClass(5);
  3. x[3] = new myClass(42);
and so on. Stupidly simple, I'm sure I'd tried that one at least once but obviously not. It is annoying that it can't be done in one line.

Cheers,

Tom
Jan 24 '07 #3
r035198x
13,262 8TB
Thanks for the reply.

I get this error with your code.
myEg.java:20: cannot find symbol
symbol : variable myClass
location: class myEg
myClass[i] = new myClass(arg)
;
myClass is the name of the class. I assume you meant:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < x.length;i++) {
  2.    x[i] = new myClass(arg);
  3. }
which compiles fine.

And I assume that even if I wanted to pass different arguments I could do so by continuing:

Expand|Select|Wrap|Line Numbers
  1. x[1] = new myClass(arg);
  2. x[2] = new myClass(5);
  3. x[3] = new myClass(42);
and so on. Stupidly simple, I'm sure I'd tried that one at least once but obviously not. It is annoying that it can't be done in one line.

Cheers,

Tom
Indeed that's what I should have wrote. Good thing you pointed it out.
Good luck with the rest of it.
Jan 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
8
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
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...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
6
by: bahoo | last post by:
Hi, I have an array that I need to define inside a function, and later use it in another function. main() { Object* arr = NULL; func1( arr );
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.