delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/09/05:58:44

Message-Id: <3.0.6.32.19990303185011.0085fc60@pop.globalserve.net>
X-Sender: derbyshire AT pop DOT globalserve DOT net (Unverified)
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Wed, 03 Mar 1999 18:50:11 -0500
To: djgpp AT delorie DOT com
From: Paul Derbyshire <pderbysh AT usa DOT net>
Subject: Re: GCC thinks there is an error when I don't.
In-Reply-To: <36DCB006.56317EDC@unb.ca>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

At 11:44 PM 3/2/99 -0400, you wrote:
>I'm not sure what "aggregate type" means...

A struct or class. Something with members, that is.

>But the lvalue is an 'int' and the rvalue is an
>'int'...

Having a look at the code, there's something duplicated...

>int main(int argc, char *argv[])
>{
> List<int> b;
> int c = b.Current().Data;
> /*Note this line.  It is assigning an 'int' to an 'int', but the compiler
>gets an error. */

The member Data is being requested from b.Current()...

>T List<T>::Current()
>{
> return Curr->Data;
> }

...but b.Current() is a T, that is, an int. Basically, you already got the
"Data" in the function, you don't need to ask for it again in main().
Because right now, you're getting the Data, which is an int, and then youre
trying to find another member Data in the int. And int has no members,
since it is a "non-aggregate type"...

There's another problem. The list b is created empty, in particular, with
Curr not even initialized (rather than initialized to 0 even). Then you
attempt to get information out of the empty List, dereferencing the bad
pointer Curr in the function Current(). When you fix the extra Data
problem, you'll find it starts producing frightening core dumps instead!

A good C++ object should initialize itself. Add a constructor to List that
sets Curr to 0, and have Current check for an empty list (Curr == 0).

You also need to put something in the list before retrieving something in
main(). So you should add an Add to your List which adds a T by making a
new Node with a T, with next pointing to where Curr points, and then
replacing Curr with a pointer to the new node...

-- 
   .*.  "Clouds are not spheres, mountains are not cones, coastlines are not
-()  <  circles, and bark is not smooth, nor does lightning travel in a
   `*'  straight line."    -------------------------------------------------
        -- B. Mandelbrot  |http://surf.to/pgd.net
_____________________ ____|________     Paul Derbyshire     pderbysh AT usa DOT net
Programmer & Humanist|ICQ: 10423848|

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019