• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Class Creation using VC++.Net

Hi I'm new to VC++.Net and as I'm learning it, I've created a Class called Shapes and when I add in Color *sColor I get the error saying "cannot declare a managed sColor in unmanaged Shape"

eg:

Class Shape
{
 public:
   int x, y, shapesize;
   Color* sColor;
  Shape(void);
virtual ~Shap(void);
private:
virtual void CreateShape(int x, int y, int size, Color* nColor);
}

how can i fix this problem.
0
maykut
Asked:
maykut
  • 7
  • 7
1 Solution
 
_ys_Commented:
Make your class managed as well

__gc class Shape
{
    // ...
};

[That's a double leading underscore]
0
 
maykutAuthor Commented:
now I get an error saying "cannot declare interior __gc pointer as a reference of Shape"
0
 
_ys_Commented:
> now I get an error saying "cannot declare interior __gc pointer as a reference of Shape"
Which is no doubt in some other class somewhere else ...

Is this part of a larger project, with a lot of crossed wires ?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
maykutAuthor Commented:
no its a simple program for a test to help me learn.
0
 
_ys_Commented:
Managed code usually requires declaring each and every class and type as managed. i.e. use __gc everywhere.

As it's a small project it's Ok to take this course of action. If it were a larger project it may not be feasible to do it.

So, just use __gc on all type definitions.
0
 
maykutAuthor Commented:
still doesn't work I still get errors this is my code so far

__gc class Block
{
public:
      int mX, mY, mBlockSize;
      __gc Color* mColor;
               //Color* mColor;
      Block(void);
      virtual ~Block(void);
private:
      void New (int x, int y,int blocksize, Color *blockcolor);
      void Clone();
      void SetPos(int x, int y);
      void Draw(Graphics* graph);
      
};

0
 
_ys_Commented:
> __gc Color* mColor;
__gc is declared on the variable, not the type >>

Color* __gc mColor;

Also, you're error suggests that class Color is not defined as __gc.

----------->-----------
#using <mscorlib.dll>

using namespace System;

__gc class Color    // --- this __gc probably missing from your code
{
public:
    Color () {}
    ~Color () {}
};

class Graphics
{
public:
    Graphics () {}
    ~Graphics () {}
};

__gc class Block
{
public:
     int mX, mY, mBlockSize;
     Color __gc *mColor;

     Block(void) {mColor = __gc new Color();}
     virtual ~Block(void) {delete mColor;}

private:
    void New (int x, int y,int blocksize, Color __gc *blockcolor) {}
    void Clone() {}
    void SetPos(int x, int y) {}
    void Draw(Graphics *graph) {}
};

int main()
{
    Block *pBlock = new Block();
//  Same as this line - implicit
//  Block __gc *pBlock = __gc new Block( );

    delete pBlock->mColor;           // if you take away all __gc from Color, the next
                                                 // line crashes the program ... __gc really works
    delete pBlock;

    return 0;
}
----------->-----------

Regarding the implicit 'Block __gc *pBlock = __gc new Block ( );' once a type is defined with __gc, all variables are automatically __gc enabled. Thus call Block could have been written:

----------->-----------
__gc class Block
{
public:
     int mX, mY, mBlockSize;
     Color *mColor;

     Block(void) {mColor = new Color();}
     virtual ~Block(void) {delete mColor;}

private:
    void New (int x, int y,int blocksize, Color *blockcolor) {}
    void Clone() {}
    void SetPos(int x, int y) {}
    void Draw(Graphics *graph) {}
};
----------->-----------

Notice the distinct absence of __gc where Color is mentioned.
0
 
maykutAuthor Commented:
It still doesn't work. I've copied and pasted it. I'm trying to create my own header files using Classes, would that make a diffrerence.
0
 
_ys_Commented:
> I'm trying to create my own header files using Classes, would that make a diffrerence.
Not to the overall effect, no.

I've split my little sample in seperate implementation and header files, and it worked just fine.

Can you post some more of youre code, to allow me to aid you further.
0
 
maykutAuthor Commented:
Ok here is my code.
H File:

#pragma once
#using <mscorlib.dll>
using namespace System;
using namespace System::Drawing;

__gc class Block
{
public:
      int mX, mY, mBlockSize;
      Color *mColor;
    Block(void){mColor=new Color();}
      virtual ~Block(void){delete mColor;}
private:
      void New (int x, int y,int blocksize, Color *blockcolor){}
      void Clone(){}
      void SetPos(int x, int y){}
      void Draw(Graphics* graph){}
      
};

CPP File

#include "StdAfx.h"
#include ".\block.h"
#using <mscorlib.dll>


Block::Block(void)
{
}



void Block::New(int x, int y, int blocksize, Color *blockcolor)
{
 
      mX=x;
      mY=y;
      mBlockSize=blocksize;
}
void Block::Clone()
{
}

void Block::SetPos(int x, int y)
{
      mX=x;
      mY=y;
}

void Block::Draw(Graphics* graph)
{
      Rectangle r;
      r=Rectangle(mX+1, mY+1, mBlockSize-1, mBlockSize-2);
      
}


0
 
_ys_Commented:
> using namespace System::Drawing;
... I'd assumed that both Color and Graphics were your own classes, and not FCL.

> Color *mColor
As you're well aware this is the line causing grief. And well it should.

Withn the FCL Color is a ValueType object - a structure. Structures however are different from their C++ counterparts. You can't allocate them with new. In fact you can't dynamically allocate them at all.

Treat it as an enumeration on steroids ...

#pragma once
#using <mscorlib.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;

__gc class Block
{
public:
    int mX, mY, mBlockSize;
    Color mColor;  //** pointer declaration removed
    Block(void) : mColor(Color::Empty) {}   //** new has been removed
    ~Block(void) {}                                    //** delete has been removed
private:
     void New (int x, int y,int blocksize, Color *blockcolor);
     void Clone();
     void SetPos(int x, int y);
     void Draw(Graphics *graph);
};
0
 
maykutAuthor Commented:
I've removed mColor(Color::Empty){} because I was getting an error saying the following " function Block::Block(void) already has a body"

Then I tested it an worked. Once I start adding in functions/procedures like void SetPos(int x, int y) it gives me a link error saying "Test error LNK2020: unresolved token (06000007) Block::SetPos"
0
 
_ys_Commented:
Try removing #pragma once from block.h
0
 
Statick001Commented:
that link error occurs for 2 reasons (that i have discovered)

either

1) you have defined a class function in its header file, but not defined the function body in the cpp file. the linker is looking for this function body and failing

2) you have included a class in your program by using #include "header.h" in your code, but have not physically added the file(s) to your visual studio project. all files must be added to the project, or the linker won't find them.

0
 
maykutAuthor Commented:
I've worked it out. Thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now