简体   繁体   中英

Undeclared identifier error in managed code…Visual C#

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:

  • Declaring a native C++ class public is not valid syntax, remove public
  • Your cm variable should be private so that the C# code cannot see it
  • You must write a finalizer for your Main() class, !Main , so that you won't leak memory when the C# programmer forgets to call Dispose().

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM