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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Thompson

    #1

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

    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\ani mals\animals.cp p(13): error C2143: syntax
    error : missing ';' before '<class-head>'
    c:\Projects\ani mals\animals.cp p(13): error C2501: '_gc' :
    missing storage-class or type specifiers
    c:\Projects\ani mals\animals.cp p(20): error C3265: cannot
    declare a managed 'strName' in an unmanaged 'animal'
    c:\Projects\ani mals\animals.cp p(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.WriteLi ne("Animal 1");
    Console.Write(" Name: ");
    Console.WriteLi ne(Cat->GetName());
    Console.Write(" Legs: ");
    Console.WriteLi ne(Cat->legs);
    Console.WriteLi ne();

    return 0;
    }

  • Nishant S

    #2
    Re: Why Doesn't This Code Work...???????? ?

    Have you enabled the managed extensions (/clr) ?

    --
    Regards,
    Nish [VC++ MVP]



    "James Thompson" <james_thompson 99@hotmail.com> wrote in message
    news:0bb701c371 6a$c2884b50$a50 1280a@phx.gbl.. .[color=blue]
    > 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\ani mals\animals.cp p(13): error C2143: syntax
    > error : missing ';' before '<class-head>'
    > c:\Projects\ani mals\animals.cp p(13): error C2501: '_gc' :
    > missing storage-class or type specifiers
    > c:\Projects\ani mals\animals.cp p(20): error C3265: cannot
    > declare a managed 'strName' in an unmanaged 'animal'
    > c:\Projects\ani mals\animals.cp p(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.WriteLi ne("Animal 1");
    > Console.Write(" Name: ");
    > Console.WriteLi ne(Cat->GetName());
    > Console.Write(" Legs: ");
    > Console.WriteLi ne(Cat->legs);
    > Console.WriteLi ne();
    >
    > return 0;
    > }
    >[/color]


    Comment

    • James Thompson

      #3
      Re: Why Doesn't This Code Work...???????? ?

      [color=blue]
      >-----Original Message-----
      >Have you enabled the managed extensions (/clr) ?
      >
      >--
      >Regards,
      >Nish [VC++ MVP][/color]

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


      [color=blue]
      >
      >
      >
      >"James Thompson" <james_thompson 99@hotmail.com> wrote in[/color]
      message[color=blue]
      >news:0bb701c37 16a$c2884b50$a5 01280a@phx.gbl. ..[color=green]
      >> Ok, I am trying to learn C++ .NET, so I went and bought
      >> Visual C++ .NET Step by Step. I started the first[/color][/color]
      project[color=blue][color=green]
      >> and I have typed in the code EXACTLY like the book tells
      >> me to. Then when I build the project, I get these[/color][/color]
      errors:[color=blue][color=green]
      >>
      >> c:\Projects\ani mals\animals.cp p(13): error C2143: syntax
      >> error : missing ';' before '<class-head>'
      >> c:\Projects\ani mals\animals.cp p(13): error[/color][/color]
      C2501: '_gc' :[color=blue][color=green]
      >> missing storage-class or type specifiers
      >> c:\Projects\ani mals\animals.cp p(20): error C3265: cannot
      >> declare a managed 'strName' in an unmanaged 'animal'
      >> c:\Projects\ani mals\animals.cp p(35): error C2143: syntax
      >> error : missing ';' before '.'
      >>
      >> Here is my code for the project that I typed exactly[/color][/color]
      from[color=blue][color=green]
      >> 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.WriteLi ne("Animal 1");
      >> Console.Write(" Name: ");
      >> Console.WriteLi ne(Cat->GetName());
      >> Console.Write(" Legs: ");
      >> Console.WriteLi ne(Cat->legs);
      >> Console.WriteLi ne();
      >>
      >> return 0;
      >> }
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Sarat Venugopal

        #4
        Re: Why Doesn't This Code Work...???????? ?

        > _gc class animal

        Hmmm... that should be:

        __gc class animal (Notice *two* underscores)

        Cheers,
        Sarat Venugopal
        Huelix Solutions
        Huelix Solutions is a maker of screen recorder and screen broadcast application, audio converter to convert audio formats between MP3, WMA, OGG, and WAV formats, and record audio in MP3, WMA, OGG, and WAV formats with PC sound hardware. The company also performs custom software development and software consulting services.


        * Remove '_ng' and 'n0spam' to reply


        "James Thompson" <james_thompson 99@hotmail.com> wrote in message
        news:0bb701c371 6a$c2884b50$a50 1280a@phx.gbl.. .[color=blue]
        > 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:[/color]

        <snip>


        Comment

        • James Thompson

          #5
          Re: Why Doesn't This Code Work...???????? ?

          [color=blue]
          >-----Original Message-----[color=green]
          >> _gc class animal[/color]
          >
          >Hmmm... that should be:
          >
          >__gc class animal (Notice *two* underscores)
          >
          >Cheers,
          >Sarat Venugopal
          >Huelix Solutions
          >www.huelix.com[/color]

          Thanks a lot, that fixed it.

          Comment

          Working...