菜鸟问题,函数可以编译生成DLL,类咋就不成呢?

czb czb 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/* 文件名:lib.h */
#include <string>


#ifndef LIB_H

#define LIB_H

int add(int x,int y);
int sub(int x,int y);

//class CPerson{
//public:
// int id;
// string name;
//public:
// void setID(int id);
// void setName(string name);
// void printID();
// void printName();
//};

#endif

#include "stdafx.h"
#include "lib.h"

int add(int x, int y)
{
return x + y;
}

int sub(int x,int y)
{
return x-y;
}


//void CPerson::setID(int id)
//{
// this.id = id;
//}
//
//void CPerson::setName(string name);
//{
// this.name = name;
//}
//
//void CPerson::printID(string name);
//{
// cout << id << endl;
//}
//
//void CPerson::printName(string name);
//{
// cout << name << endl;
//}

如上面的文件内容,单单编译add、sub函数是可以编译成DLL的,不过如果把类的注释去掉,就死都编译不过了,报错"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"。即使把add、sub函数的声明和实现去掉,仅留下类CPerson,也编译不过。

编译器是vs2005

我现在是函数和类都要保留,因为类不能导出给其它语言调用,所以就转了个弯,先弄个函数,在函数里调用类。如果哪位大虾除了COM之外还有其它的方法,也请指出。

...全文
评论 10
收藏 已收藏
分享
返回顶部
czb
进程/线程/DLL 15,466 成员 49,169 内容