Re: Can't compile this code

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 11/25/04


Date: Thu, 25 Nov 2004 22:18:49 +1100


"Morten Gulbrandsen" <Morten.Gulbrandsen@rwth-aachen.de> wrote in message
news:30ls7cF30n7etU1@uni-berlin.de...
| #include <iostream>

[snip]

I thought I gave you all the clues you needed :-)

| int main()
| {
| Cat frisky = new Cat(5,3); // cannot convert from 'class Cat *' 'class

Here is your first problem.

The operator 'new', returns a pointer to allocated
memory, if it was successful, otherwise it throws
an 'std::bad_alloc()' exception, in it's default
mode of operation.

So if 'new' returns a pointer to memory for a 'Cat'
object, then we need to store that returned pointer
in a 'Cat*' object type, therefore, what we need to
do is this:

Cat* PtrToCat = new Cat( 5, 3 );

Since we are now working with a pointer to a 'Cat' object,
the rules have changed slightly, in the way that we access
it's internal parts. In this case, we need to use a very
different member access operator '->' instead, thus we can
now complete our task with:

PtrToCat -> Meow( 3 );

| Cat'
| frisky.Meow(3);
|
| return 0;
| }
|
|
| ===
|
| Only one error left,
|
| What can I do ?

[snip]

You could purchase some good beginner books.

Do you have any ?

Perhaps we can offer some suggestions, if you
indicate your level of experience(s) ?

Cheers.
Chris Val



Relevant Pages

  • Re: structure and constant memebers
    ... > own development practices are so strongly predisposed to never ... >> No one should object to the cast as such, which is clearly a pointer ... but for reasons that are ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... developing it on Windows I don't know if there's ... mingw uses the Windows libraries, and the Windows equivalent to the ... Note that in this, similar, case, you got it right: you pass a pointer ...
    (comp.lang.c)
  • Re: [Lit.] Buffer overruns
    ... Or the pointer gets wrong or the wrong buffer is pointed ... >young dudes about the head. ... My first few programming classes ...
    (sci.crypt)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... Question 10 fails to account for pointer comparison, ... But C++ structs can indeed have member functions, ...
    (comp.programming)
  • Re: typecast internals
    ... (snip of description of floating point formats) ... >>that the value is of the appropriate pointer type, ... > much of a conversion operation as arithmetic-type casts. ...
    (comp.lang.c)