ITPub博客

首页 > 应用开发 > > 加载DLL的示例C++程序出现错误!大神请进。

加载DLL的示例C++程序出现错误!大神请进。

作者:yesye 时间:2019-08-14 15:38:11 0 删除 编辑

自己编写了一个试样程序,用来测试编写的类是否正常运行。

思路:将该类在dll中导出,在控制台程序中隐式加载该dll(具体操作就是lib+dll的方法),然后就是正常调用该类的功能。尤其,我在类中自定义了迭代器,应该支持范围for(range for)操作。

Note:内含CLeap类和它的容器类CLeaps类;该类主要实现值班跳岗原因的记录功能。

出现错误如下:

就是这样,提示vector无法解引用!!!

附上代码控制台程序代码:

1 // use-dll.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "DLL_Leaps.h"
 6 
 7 int _tmain(int argc, _TCHAR* argv[])
 8 {
 9     CLeaps myleaps;
10     myleaps.setfile("data.dat");
11     //初始化一个leap
12     CLeap myleap0(string("do activity_0"),time(nullptr),false);
13     CLeap myleap1(string("do activity_1"),time(nullptr),false);
14     CLeap myleap2(string("do activity_2"),time(nullptr),false);
15     myleaps.append(myleap0);
16     myleaps.append(myleap1);
17     myleaps.append(myleap2);
18     cout<<std::setiosflags(ios::left)
19         <<setw(20)<<"reason"
20         <<setw(20)<<"time"
21         <<setw(20)<<"execute_state"
22         <<endl;
23 
24     for(auto obj=myleaps.begin();obj!=myleaps.end();obj++)
25     {
26         leapRecord r=obj->enquireRecord();
27         char t[255];
28         ctime_s(t,255,&r.time);
29         cout<<std::setiosflags(ios::left)
30             <<setw(20)<<r.reason 
31             <<setw(20)<<t
32             <<setw(20)<<r.executed
33             <<endl;
34     }
35 
36     bool res=myleaps.save();
37     if (!res) cout<<"save fail!"<<endl;
38     else cout<<"save sucessed!"<<endl;
39 
40     system("pause");
41     return 0;
42 }
其中DLL_Leaps.h就是dll的类声明:

 1 // 下列 ifdef 块是创建使从 DLL 导出更简单的
 2 // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DLL_LEAPS_EXPORTS
 3 // 符号编译的。在使用此 DLL 的
 4 // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
 5 // DLL_LEAPS_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
 6 // 符号视为是被导出的。
 7 #ifdef DLL_LEAPS_EXPORTS
 8 #define DLL_LEAPS_API __declspec(dllexport)
 9 #else
10 #define DLL_LEAPS_API __declspec(dllimport)
11 #endif
12 
13 // 此类是从 DLL_Leaps.dll 导出的
14 #include "Leap.h"
15 #include "Leaps.h"
Leaps.h文件:

 1 #ifndef LEAPS_H
 2 #define LEAPS_H
 3 
 4 #include "Leap.h"
 5 #include <vector>
 6 using std::vector;
 7 #include <fstream>
 8 using std::fstream;
 9 using std::ios;
10 //为实现文件删除,改名操作
11 #include <stdio.h>
12 
13 struct leapRecord;
14 
15 struct DLL_LEAPS_API leapRecords
16 {
17     //标识输出到文件的大小而非对齐后大小
18     int cbsize;
19     leapRecord *records;
20 };
21 
22 class DLL_LEAPS_API CLeaps
23 {
24 public:
25     CLeaps():leaps(0)
26     {
27         now=nullptr;
28         records.cbsize=0;
29         records.records=nullptr;
30     }
31 
32     ~CLeaps(void);
33 
34     //请求跳岗记录集
35     leapRecords enquireRecords();
36     //销毁传输使用的记录集
37     void destroyRecords();
38 
39     //自定义迭代器
40     typedef CLeap* iterator;
41 
42     void append(CLeap& leap);
43     void remove(const CLeap& leap);
44     void clear(void);
45 
46     int size()const;
47 
48     iterator begin();
49     iterator end();
50 
51     CLeap& operator[](int selector);
52 
53     iterator operator++();
54     iterator operator++(int);
55     iterator operator--();
56     iterator operator--(int);
57     CLeap& operator*();
58     bool operator==(iterator& ite);
59     bool operator!=(iterator& ite);
60 
61     //文件保存位置
62     void setfile(string f);
63     const string& getfile()const;
64 
65     bool save();
66     bool load();
67 
68 private:
69 
70     //CLeap组
71     vector<CLeap> leaps;
72     //支持迭代器,标记当前位置
73     iterator now;
74     //用来输出跳岗记录集
75     leapRecords records;
76 
77     string file;
78 };
79 
80 #endif
Leap.h头文件如下:

复制代码
 1 #ifndef LEAP_H
 2 #define LEAP_H
 3 
 4 #include <string>
 5 #include <ctime>
 6 using std::string;
 7 
 8 struct DLL_LEAPS_API leapRecord
 9 {
10     bool executed;
11     char reason[255];
12     long long time;
13 };
14 
15 class DLL_LEAPS_API CLeap
16 {
17 public:
18     CLeap(){}
19     CLeap(string rsn,time_t timestamp,bool flag)
20         :reason(rsn),recordtime(timestamp),hasExecuted(flag){}
21     ~CLeap(void);
22 
23     //请求跳岗记录
24     leapRecord enquireRecord();
25 
26     //更新跳岗执行状态:true为已跳岗
27     void updateExecuteStat(bool stat){hasExecuted=stat;}
28     //获取跳岗状态
29     bool getExecuteStat()const{return hasExecuted;}
30 
31     bool operator==(const CLeap& leap)const;
32     bool operator!=(const CLeap& leap)const{return !(operator==(leap));}
33 
34 private:
35     //跳岗原因
36     string reason;
37     //记录生成时间
38     time_t recordtime;
39     //标识是否已经跳岗
40     bool hasExecuted;
41 };
42 
43 #endif

如果需要dll的实现代码,我再附上。

当然,由于导出类中含有vetctor,string这种含模板的STL类,毫无疑问会warning,我直接选择无视了。不知道是否对这个bug有所影响。

C++现在越来越少人关注它,以至于问题抛出之后经常石沉大海,作为一个C++的新手,我还是对大神突然降临满怀期待的!

大神啊,救救我这团团转的小程序猿吧!

C++DLL

问题补充:Tip:我的感觉是迭代器定义出现问题了。所以导致无法解引用。

上述我定义的

1 //该运算符重载是在leaps.h中实现的

2 CLeap& operator*();

所以对迭代器隐式解引用肯定会失败!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2041/viewspace-3164290/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 关于F#的解释
请登录后发表评论 登录
全部评论

注册时间:2019-09-06

  • 博文量
    139
  • 访问量
    9682