Solved

Class Creation using VC++.Net

Posted on 2004-08-16
16
202 Views
Last Modified: 2008-02-01
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
Comment
Question by:maykut
  • 7
  • 7
16 Comments
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
Make your class managed as well

__gc class Shape
{
    // ...
};

[That's a double leading underscore]
0
 
LVL 1

Author Comment

by:maykut
Comment Utility
now I get an error saying "cannot declare interior __gc pointer as a reference of Shape"
0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
> 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
 
LVL 1

Author Comment

by:maykut
Comment Utility
no its a simple program for a test to help me learn.
0
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
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
 
LVL 1

Author Comment

by:maykut
Comment Utility
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
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
> __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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:maykut
Comment Utility
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
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
> 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
 
LVL 1

Author Comment

by:maykut
Comment Utility
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
 
LVL 9

Accepted Solution

by:
_ys_ earned 20 total points
Comment Utility
> 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
 
LVL 1

Author Comment

by:maykut
Comment Utility
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
 
LVL 9

Expert Comment

by:_ys_
Comment Utility
Try removing #pragma once from block.h
0
 
LVL 3

Expert Comment

by:Statick001
Comment Utility
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
 
LVL 1

Author Comment

by:maykut
Comment Utility
I've worked it out. Thanks guys.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now