读书人

种的一个成员变量也是类老提示没有构

发布时间: 2012-09-09 09:27:54 作者: rapoo

类的一个成员变量也是类,老提示没有构造函数
这是com本质论1.5节的实例 接口和实现分离

FastString是实现类,FastStringItf是接口类。要从DLL只导出FastStringItf类。只能给用户一个dll和FastStringItf的头文件,不能让用户感觉FastString类的存在。

C/C++ code
//faststring.h是FastString类的声明class FastString{    char *m_psz;public:    FastString(const char *psz);    ~FastString(void);    int Length(void)const;    int Find(const char ch)const;};//faststring.cpp是FastString类的实现#include "FastString.h"#include <string.h>FastString::FastString(const char *psz)    :m_psz(new char[strlen(psz)+1]){    strcpy(m_psz,psz);}//这里省去不必展示的代码//FastStringItf.h是FastStringItf的声明#ifdef DLLEXPORT#define  DLLEXPORT __declspec(dllexport)#else#define DLLEXPORT __declspec(dllimport)#endifclass DLLEXPORT FastStringItf{    class FastString;  //导入实现类的名称    FastString *m_pThis;public:    FastStringItf(const char *psz);    ~FastStringItf(void);    int Length(void)const;    int Find(const char ch)const;};//FastStringItf.cpp是类FastStringItf的实现#include "FastString.h"#include "FastStringItf.h"#include <assert.h>#include <Windows.h>BOOL APIENTRY DLLMain(HMODULE hModule,DWORD ul_reason_for_call,    LPVOID lpReserved){    switch (ul_reason_for_call)    {    case DLL_PROCESS_ATTACH:    case DLL_THREAD_ATTACH:    case DLL_THREAD_DETACH:    case DLL_PROCESS_DETACH:        break;    }    return TRUE;}FastStringItf::FastStringItf(const char *psz)    :m_pThis(new FastString(psz))//就是这里老提示FastString没有构造函数{    assert(m_pThis!=NULL);}//这里省去不必展示的代码



1>g:\vs2010projects\faststringdll\faststringdll\faststringitf.cpp(23): warning C4273: “FastStringItf::FastStringItf”: dll 链接不一致
1> g:\vs2010projects\faststringdll\faststringdll\faststringitf.h(12) : 参见“{ctor}”的前一个定义
1>g:\vs2010projects\faststringdll\faststringdll\faststringitf.cpp(23): error C2514: “FastStringItf::FastString”: 类没有构造函数
1> g:\vs2010projects\faststringdll\faststringdll\faststringitf.h(9) : 参见“FastStringItf::FastString”的声明
1>g:\vs2010projects\faststringdll\faststringdll\faststringitf.cpp(24): fatal error C1903: 无法从以前的错误中恢复;正在停止编译
1>

[解决办法]
原来在.cpp文件里包含了。忽略一楼的回答。
[解决办法]
探讨
这是com本质论1.5节的实例 接口和实现分离

FastString是实现类,FastStringItf是接口类。要从DLL只导出FastStringItf类。只能给用户一个dll和FastStringItf的头文件,不能让用户感觉FastString类的存在。


C/C++ code

//faststring.h是FastString类的声明
class FastStrin……

读书人网 >C++

热点推荐