Mail Archives: djgpp/2000/04/25/19:43:27
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 -