Why Doesn't This Code Work...?????????

J

James Thompson

Ok, I am trying to learn C++ .NET, so I went and bought
Visual C++ .NET Step by Step. I started the first project
and I have typed in the code EXACTLY like the book tells
me to. Then when I build the project, I get these errors:

c:\Projects\animals\animals.cpp(13): error C2143: syntax
error : missing ';' before '<class-head>'
c:\Projects\animals\animals.cpp(13): error C2501: '_gc' :
missing storage-class or type specifiers
c:\Projects\animals\animals.cpp(20): error C3265: cannot
declare a managed 'strName' in an unmanaged 'animal'
c:\Projects\animals\animals.cpp(35): error C2143: syntax
error : missing ';' before '.'

Here is my code for the project that I typed exactly from
the book.......can anyone figure out why this would not
run?


// This is the main project file for VC++ application
project
// generated using an Application Wizard.

#include "stdafx.h"
#include <string.h>

#using <mscorlib.dll>
#include <tchar.h>

using namespace System;

_gc class animal
{
public:
int legs;
void SetName(String *Name)
{strName = strName->Copy(Name);};
String* GetName() {return strName;};
private:
String *strName;
};

int _tmain(void)
{
animal *Cat, *Dog;

Cat = new animal;
Dog = new animal;

Cat->SetName("Cat");
Cat->legs = 4;
Dog->SetName("Dog");
Dog->legs = 4;

Console.WriteLine("Animal 1");
Console.Write("Name: ");
Console.WriteLine(Cat->GetName());
Console.Write("Legs: ");
Console.WriteLine(Cat->legs);
Console.WriteLine();

return 0;
}
 
J

James Thompson

-----Original Message-----
Have you enabled the managed extensions (/clr) ?

I don't know, the book doesn't say I have to do that.
How would I do that anyways?
 
S

Sarat Venugopal

_gc class animal

Hmmm... that should be:

__gc class animal (Notice *two* underscores)

Cheers,
Sarat Venugopal
Huelix Solutions
www.huelix.com

* Remove '_ng' and 'n0spam' to reply


James Thompson said:
Ok, I am trying to learn C++ .NET, so I went and bought
Visual C++ .NET Step by Step. I started the first project
and I have typed in the code EXACTLY like the book tells
me to. Then when I build the project, I get these errors:

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top