繁体   English   中英

托管代码中未声明的标识符错误…Visual C#

[英]Undeclared identifier error in managed code…Visual C#

我有一个使用C ++ / CLI代码的C#项目。 我在C ++ / CLI中有一个名为Main的托管类,而我有一个名为codemain的非托管类。 在C#中,我创建了一个托管类的对象并使用它,但是在运行项目时遇到一些错误-大多数错误是: cm : undeclared identifier

我所有的代码都是seaCV命名空间。 当我在托管类( Main )之外的seaCV命名空间中定义cm对象时,我的项目运行没有错误,但是当我在托管类中定义cm ,我得到了该错误。 问题出在哪儿?

我在seaCV.h文件中的托管类:

#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);    
    };
}

seaCV.cpp文件:

#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;
    }       
}

最后,我的非托管代码在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"

那是你的问题。 编译器将在seaCV.h文件中看到“ codemain”,但不知道其含义。 您必须交换两个#include。

请记住,与C#编译器不同,C ++编译器是单遍编译器。 在使用标识符之前,需要先查看标识符的定义。

您的代码中的几个小问题:

  • 声明本机C ++类public是无效语法,请删除public
  • 您的cm变量应为私有变量,以便C#代码看不到它
  • 您必须为Main()类!Main编写一个终结器,以便在C#程序员忘记调用Dispose()时不会泄漏内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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