maykut
asked on
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.
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.
ASKER
now I get an error saying "cannot declare interior __gc pointer as a reference of Shape"
> 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 ?
Which is no doubt in some other class somewhere else ...
Is this part of a larger project, with a lot of crossed wires ?
ASKER
no its a simple program for a test to help me learn.
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.
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.
ASKER
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);
};
__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);
};
> __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.
__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.
ASKER
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.
> 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.
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.
ASKER
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);
}
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"
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"
Try removing #pragma once from block.h
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.
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.
ASKER
I've worked it out. Thanks guys.
__gc class Shape
{
// ...
};
[That's a double leading underscore]