Solved

Class Creation using VC++.Net

Posted on 2004-08-16
16
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
16 Comments
 
LVL 9

Expert Comment

by:_ys_
ID: 11818031
Make your class managed as well

__gc class Shape
{
    // ...
};

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

Author Comment

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

Expert Comment

by:_ys_
ID: 11818102
> 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
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
LVL 1

Author Comment

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

Expert Comment

by:_ys_
ID: 11830334
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
ID: 11837503
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_
ID: 11850049
> __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
 
LVL 1

Author Comment

by:maykut
ID: 11876522
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_
ID: 11878576
> 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
ID: 11887812
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
ID: 11889880
> 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
ID: 11898910
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_
ID: 11900154
Try removing #pragma once from block.h
0
 
LVL 3

Expert Comment

by:Statick001
ID: 12054744
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
ID: 12116812
I've worked it out. Thanks guys.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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