delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/04/25/19:43:27

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help Me!
Date: 25 Apr 2000 13:04:28 -0700
Organization: InterWorld Communications
Lines: 57
Message-ID: <83zoqioskz.fsf@mercury.st.hmc.edu>
References: <200004240500 DOT BAA26982 AT delorie DOT com> <8e416p$v2k$1 AT nnrp1 DOT deja DOT com>
NNTP-Posting-Host: mercury.st.hmc.edu
Mime-Version: 1.0
X-Trace: nntp1.interworld.net 956693356 41342 134.173.45.219 (25 Apr 2000 20:09:16 GMT)
X-Complaints-To: usenet AT nntp1 DOT interworld DOT net
NNTP-Posting-Date: 25 Apr 2000 20:09:16 GMT
User-Agent: Gnus/5.0802 (Gnus v5.8.2) Emacs/20.5
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

ahj372 AT my-deja DOT com writes:

> I've run into 2 problems recently!
> 
> An example of the first:
> 
> class BADGUY
> {
> public:
> 
> BADGUY *next=0;
> 
> private:
> };
> 
> int main()
> {
>  return 0;
> }
> 
> This will not compile!?! I'm intending BADGUY to
> be a linked list hence the pointer to next.

The error is:

foo.cc:5: ANSI C++ forbids initialization of member `next'
foo.cc:5: making `next' static
foo.cc:5: ANSI C++ forbids in-class initialization of non-const static member `next'

So you can't initialize it like that.  Understandable, since it would
have to be done for every instance of BADGUY, many of which are
created on the fly.  The correct way, I believe, is to write a
constructor which does this initialization.

> 2nd prob:
> 
> I can't seem to figure out how to make a function
> return an array such as a string of characters

You can't return an array.  You must instead return a pointer.  Note
that the pointer can *point* to the array.  Example:

char *foo(void)
{
  static char str[] = "Hello world";
  return str;
}

Any good book on C++ should explain this.

Also note the static; be careful never to return pointers to auto
variables, since they disappear when the function returns!

-- 

Nate Eldredge
neldredge AT hmc DOT edu

- Raw text -


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