C++调用DLL expected constructor, destructor, or type conversion before ('token

lizhongheng 2018-12-14 11:10:45
最近在有一个Win32项目,用到了Redis, 按照网上的方法用VS2017封装了一个Dll, 在VS环境下调用正常,使用g++编译时报错了, 错误如下

redis.h:13 error: expected constructor, destructor, or type conversion before ‘('token

这是临时交接给我的项目,我原来也没做过C++开发,请各位给看看,问题在哪里?


redis.h


#pragma once
#include <string>

using namespace std;
class RedisBase
{
public:
virtual ~RedisBase() {};
virtual void Init() = 0;
virtual void Init(char* ip, int port) = 0;
virtual char* Command(char* cmd, bool needresult = false) = 0;
};

extern "C" _declspec(dllexport) RedisBase* GetInstance(); //错误在这一行


dll的封装资料来源:

https://blog.csdn.net/nightwizard2030/article/details/72829697

以下是Dll代码:

redis.h


#pragma once
#include "stdafx.h"
#include "hiredis/hiredis.h"
#define NO_QFORKIMPL //这一行必须加才能正常使用
#include "Win32_Interop\win32fixes.h"
#pragma comment(lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib")
using namespace std;
class RedisBase
{
public:
virtual ~RedisBase() {};
virtual void Init() = 0;
virtual void Init(char* ip, int port) = 0;
virtual char* Command(char* cmd, bool needresult = false) = 0;
};


extern "C" _declspec(dllexport) RedisBase* GetInstance();


redis.cpp


// redis.cpp : 定义 DLL 应用程序的导出函数。
//

#include "redis.h"
#include <iostream>
#include "atomic_lock.h"

using namespace std;
class Redis :public RedisBase
{
private:
redisContext *context;
Atomic_Lock lock;
public:
virtual void Init()
{
context = redisConnect("127.0.0.1", 6379);
redisEnableKeepAlive(context);
if (context->err) {
printf("Connection error: %s\n", context->errstr);
exit(1);
}
}
virtual void Init(char* ip, int port)
{

context = redisConnect(ip, port);
redisEnableKeepAlive(context);
if (context->err) {
printf("Connection error: %s\n", context->errstr);
exit(1);
}
}
virtual ~Redis()
{
redisFreeKeepFd(context);
redisFree(context);
}
virtual char* Command(char* cmd, bool needresult = false)
{
lock.Lock();
redisReply* reply = (redisReply *)redisCommand(context, cmd);
if (!needresult)
{
freeReplyObject(reply);
lock.UnLock();
return NULL;
}
char* result = NULL;
if (reply->len > 0)
{
result = new char[reply->len + 1];
memcpy(result, reply->str, reply->len);
result[reply->len] = '\0';
}
freeReplyObject(reply);
lock.UnLock();
return result;
}
};

RedisBase* GetInstance()
{
return new Redis();
}


...全文
750 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhongheng 2019-12-30
  • 打赏
  • 举报
回复
引用 7 楼 Saleayas 的回复:
__declspec
谢谢,正解
Saleayas 2018-12-16
  • 打赏
  • 举报
回复
__declspec
lizhongheng 2018-12-15
  • 打赏
  • 举报
回复
引用 5 楼 zjq9931 的回复:
改成这样试试?

RedisBase* GetInstance()
{
    return new Redis; // 去掉括号。
}
还是报同样的错误
srhouyu 2018-12-14
  • 打赏
  • 举报
回复
我这里没有问题
  • 打赏
  • 举报
回复
改成这样试试?

RedisBase* GetInstance()
{
    return new Redis; // 去掉括号。
}
lizhongheng 2018-12-14
  • 打赏
  • 举报
回复
问题就是: 在VS2017正常编译,用g++命令行时出错
srhouyu 2018-12-14
  • 打赏
  • 举报
回复
引用 2 楼 lizhongheng 的回复:
是用g++命令行编译的吗?
我用的是VS 2017
lizhongheng 2018-12-14
  • 打赏
  • 举报
回复
是用g++命令行编译的吗?

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧