错误C2491:在标头中声明并在C ++中定义的函数 [英] Error C2491: Function declared in header and defined in C++

查看:152
本文介绍了错误C2491:在标头中声明并在C ++中定义的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为称为Micro-manager的开源软件构建设备适配器以控制显微镜,但是我遇到了一些问题,这两个文件(一个标头和另一个CPP)已经存在于Micro-Manager的开源软件包中。

I have been trying to build a device adapter for a open-source software called Micro-manager to control a microscope and there are some problems that I am facing, there are these two files (one header and the other CPP) that are already present in the open source package of Micro-Manager.

//MoudluleInterface.h

#ifndef _MODULE_INTERFACE_H_
#define _MODULE_INTERFACE_H_

#ifdef WIN32
#ifdef MODULE_EXPORTS
  #define MODULE_API __declspec(dllexport)
#else
  #define MODULE_API __declspec(dllimport)
#endif

#else
#define MODULE_API
#endif
#define MM_MODULE_ERR_OK 1000
#define MM_MODULE_ERR_WRONG_INDEX   1001
#define MM_MODULE_ERR_BUFFER_TOO_SMALL 1002

 ///////////////////////////////////////////////////////////////////////////////
 // header version
 // NOTE: If any of the exported module API calls changes, the interface version
 // must be incremented
 // new version 5 supports device discoverability
 #define MODULE_INTERFACE_VERSION 7

#ifdef WIN32
const char* const LIB_NAME_PREFIX = "mmgr_dal_";
#else
const char* const LIB_NAME_PREFIX = "libmmgr_dal_";
#endif

#include "MMDevice.h"

///////////////////////////////////////////////////////////////////////////////
// Exported module interface
///////////////////////////////////////////////////////////////////////////////
 extern "C" {
 MODULE_API MM::Device* CreateDevice(const char* name);
 MODULE_API void DeleteDevice(MM::Device* pDevice);
 MODULE_API long GetModuleVersion();
 MODULE_API long GetDeviceInterfaceVersion();
 MODULE_API unsigned GetNumberOfDevices();
 MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned      bufferLength);
 MODULE_API bool GetDeviceDescription(const char* deviceName, char* name, unsigned bufferLength);

这是CPP文件的一部分,定义了这些功能

And here is a part of the CPP file which defines these functions

      //ModuleInterface.cpp
      #define _CRT_SECURE_NO_DEPRECATE
      #include "ModuleInterface.h"
      #include <vector>
      #include <string>

        typedef std::pair<std::string, std::string> DeviceInfo; 
        std::vector<DeviceInfo> g_availableDevices;

      int FindDeviceIndex(const char* deviceName)
   {
  for (unsigned i=0; i<g_availableDevices.size(); i++)
  if (g_availableDevices[i].first.compare(deviceName) == 0)
     return i;

   return -1;
   }

  MODULE_API long GetModuleVersion()
 {
 return MODULE_INTERFACE_VERSION;   
 }

  MODULE_API long GetDeviceInterfaceVersion()
 {
return DEVICE_INTERFACE_VERSION;   
 }

 MODULE_API unsigned GetNumberOfDevices()
{
 return (unsigned) g_availableDevices.size();
 }

 MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned bufLen)
{
 if (deviceIndex >= g_availableDevices.size())
  return false;

现在的问题是它给我一个错误C2491(不允许定义dllimport函数)
我对此进行了研究,通常是在应该声明函数的时候定义了函数,我已经在ModuleInterface.h中定义了该函数,然后在ModuleInterface.cpp中使用了该函数,但是仍然显示相同的错误。
可能还会有其他可能性发生此错误吗?还是代码有问题?

Now the problem is it gives me an error C2491 (definition of dllimport function not allowed) I did research about this and it usually is when a function is defined when it is supposed to be declared, I have already defined the function in ModuleInterface.h and then used it in ModuleInterface.cpp but it still shows the same error. Can there be some other possibility for this error to occur? Or is there something wrong with the code?

推荐答案

您不应在定义中重复您的MODULE_API声明,作为声明的一部分就足够了。从.cpp文件中删除对MODULE_API的使用,该代码应进行编译。

You're not supposed to repeat your MODULE_API declaration in the definition, having it as part of the declaration is good enough. Remove the use of MODULE_API from the .cpp file and the code should compile.

这篇关于错误C2491:在标头中声明并在C ++中定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆