[comp.lang.c++] using new to allocate an array of objects

omtzigt-theo@CS.YALE.EDU (Erwinus Theodorus Leonardus Omtzigt (Theo)) (10/10/90)

Can anyone help this novice?
I am trying to allocate an array of a user defined class along the
lines of allocating a, say, char array.
So, I try to mimic
	char *string = new char[15];
with
	TimeSlot *TimingWheel = new TimeSlot[TimingWheelSize];

The compiler, however, complaints about an illegal assignment;
	TimeSlot * = TimeSlot

What am I doing wrong?

Theo

-- 
InterNet: omtzigt-theo@cs.yale.edu	mail: Becton Center, 15 Prospect Str
Phone:	  (203) 432 - 4261		      Yale University, Dept. of EE

sarima@tdatirv.UUCP (Stanley Friesen) (10/12/90)

In article <26597@cs.yale.edu> omtzigt-theo@CS.YALE.EDU (Erwinus Theodorus Leonardus Omtzigt (Theo)) writes:
>I am trying to allocate an array of a user defined class along the
>lines of allocating a, say, char array.
>So, I try to mimic
>	char *string = new char[15];
>with
>	TimeSlot *TimingWheel = new TimeSlot[TimingWheelSize];
 
>The compiler, however, complaints about an illegal assignment;
>	TimeSlot * = TimeSlot

What are you doing wrong??   I haven't the faintest!  This looks like a
compiler bug to me.

I have used the same syntax myself with no problem!!

Anyone out there have *any* clues.





-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)

dfoster@jarthur.Claremont.EDU (Derek R. Foster) (10/14/90)

In article <24@tdatirv.UUCP> sarima@tdatirv.UUCP (Stanley Friesen) writes:
>In article <26597@cs.yale.edu> omtzigt-theo@CS.YALE.EDU (Erwinus Theodorus Leonardus Omtzigt (Theo)) writes:
>>I am trying to allocate an array of a user defined class along the
>>lines of allocating a, say, char array.
>>So, I try to mimic
>>	char *string = new char[15];
>>with
>>	TimeSlot *TimingWheel = new TimeSlot[TimingWheelSize];
> 
>>The compiler, however, complaints about an illegal assignment;
>>	TimeSlot * = TimeSlot
>
>What are you doing wrong??   I haven't the faintest!

I can see what you are doing wrong (after my little speech). My question is
more along the lines of "what on earth are you trying to do?"

<mount soapbox>
The reason I am posting this to the net is that I wish other posters to
notice what is missing from this and other recent articles of its type:
relevant details! The author does not give enough information in the
article for a reader to be able to discern what he/she is trying
to do, and with what equipment. Therefore, a reader is not capable of
solving the problem without taking wild guesses at what was meant. In this
particular example, for instance, not enough information is given about
the types of the variable (is it a variable?) TimeSlot for a reasoned guess
as to what the second piece of code is supposed to accomplish. There have
been a lot of other kinds of missing relevant information on   Other posters
have frequently left out other types of potentially useful information, and
have, as a result, caused other people to waste a great deal of bandwidth
either asking for clarifications or taking wrong guesses as to the cause
of problems because not enough information was given.

Please, posters! Include as a MINIMUM in a "why doesn't this
work" question:

0) What sort of thing is this chunk of code supposed to accomplish?
   Does it sort integers? Balance the national debt? Draw pretty
   pictures? What is it a part of? This will help readers understand
   your code faster, and waste less of their time in replying.
1) What constraints does it have to operate in? Speed? Memory?
   complexity? This will help readers avoid suggestions which you
   can't use.
2) The types of ALL variables mentioned. Int or unsigned? Class or
   non-class? Are member functions virtual or non-virtual? Is it a pointer?
   If it is a class, what are the *exact* definitions of any relevant
   member functions/variables? 
3) what kind of C/C++/other language are you running? "My version of
   C++" is NOT ENOUGH. Is that Turbo? Cfront? What version?
4) EXACTLY what error messages did the compiler give you? Saying
   "the compiler complains about line X" or "The compiler says something
   like ..." is not as useful as saying, for instance, "Turbo C++ reports 
   an 'illegal initialization error of variable xyzzy on line X', with
   the error cursor positioned after the first '&'."
5) For problems which exist only some of the time: What particular values
   of variables/conditions in memory/compiler switches/etc will make the
   problem definitely happen? not happen? Is there anything which makes
   it more likely?
6) Give us some general indication of how experienced you are in C,
   C++, object oriented languages, or programming languages. This
   may tell someone else what sort of errors to watch for. A novice might
   fall afowl of certain errors which would never be made by an experienced
   programmer, and there's no point in your readers wasting bandwidth
   suggesting possible errors which you wouldn't have made, or looking
   too deep into problems when the mistake is really very simple.
7) Anything else which you think might be related.
<dismount soapbox>

Anyway, for specific help to the poster now that I've had my 0.02c:
As I look at the second assignment, I notice that it doesn't make
syntactic sense in either C or C++. Anyway, I have a number
of questions which might help you clarify what you are trying to do:

1) Is TimeSlot a type or the name of a variable?
2) If it is the name of a variable, is it a pointer, or not?
 
If it is a variable type, then using it by itself (the second time in the
assigment statement you cite) is meaningless. It would be like saying 
"int * = int". (Just what would you be trying to assign from or to in such
a case?) 

If it is a simple variable name, then the only way that I can see that the
assignment can be made to make any kind of sense is if it was meant to be
written "TimeSlot *= TimeSlot" (note lack of a space between * and =) and
TimeSlot was some type for which the operation "*=" was meaningful.

If it is a pointer type, the statement is clearly contradictory, since
you could not assign an object to a pointer to itself or vice versa. Also,
you should use "*TimeSlot" instead of "TimeSlot *" which is meaningless
except in declarations of variables.

In the first example, TimeSlot is clearly a variable type, (you are assigning
a value to a variable of type (TimeSlot *) called TimingWheelSize), so
I am still rather confused as to what this statement is supposed to do.
You might try writing it out in English.

To the original poster: sorry for using you as an opportunity to preach, but
this has been driving me crazy lately.

To other posters: Please try to keep this in mind when you post. Give
more information than you think you need to! It really does help!

Derek Riippa Foster