I have a C# project that uses C++/CLI code. I have a managed class named Main
in C++/CLI and I have an unmanaged class named codemain
. In C#, I make an object of managed class and use it but When I run my project I get some errors--most of them are: cm : undeclared identifier
.
All of my code is the seaCV
namespace. When I define cm
object in the seaCV
namespace outside of the managed class ( Main
), my project runs without error but when I define cm
within the managed class I get that error. Where is the problem?
My managed class in seaCV.h
file:
#pragma once
#include <iostream>
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
using namespace System;
namespace seaCV
{
public ref class Main
{
public:
codemain *cm;
Main();
~Main();
void initizalize(int x, int y, int x2, int y2, int tr_ind);
};
}
and the seaCV.cpp
file:
#include "seaCV.h"
#include "codemain.h"
namespace seaCV
{
void Main::initizalize (int x, int y, int x2, int y2, int trix) {
cm->init(x,y,x2,y2,trix);
}
Main::Main() {
cm=new codemain();
}
Main::~Main() {
delete cm;
cm=0;
}
}
Finally, my unmanaged code is in codemain.h
:
#pragma once
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
namespace seaCV
{
public class codemain
{
public:
int xc,yc,xr,yr;
codemain(void) {
xc = xr = yc = yr = 1;
}
void init(int x, int y, int x2, int y2, int tracker_index) {
xc = x;
yc = y;
xr = x2;
yr = y2;
...
}
};
}
#include "seaCV.h"
#include "codemain.h"
That's your problem. The compiler will see "codemain" in your seaCV.h file but doesn't know what it means. You have to swap the two #includes.
Keep in mind that the C++ compiler is a single-pass compiler, unlike the C# compiler. It needs to see a definition of an identifier before it can be used.
Several niggly little problems in your code:
!Main
, so that you won't leak memory when the C# programmer forgets to call Dispose(). Check this link out on using unmanaged code in c#:
http://www.codeproject.com/Articles/1392/Using-Unmanaged-code-and-assembler-in-C
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.