当前位置: 首页 > news >正文

定义类方法的错误总结

struct Renderer
{vector<function<void(vector<string>)>> fileDropListeners;// 定义一个方法,它是将一个函数作为输入,callback是形参void print(function<void(float)> callback_func);void onFileDrop(function<void(vector<string>)> callback) {fileDropListeners.push_back(callback);}
};void print(function<void(float)> callback_func) {float a;std::cin >> a;callback_func(a);
}

出现错误:

Build started at 22:10...
1>------ Build started: Project: Project1, Configuration: Debug x64 ------
1>Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol "public: void __cdecl Renderer::print(class std::function<void __cdecl(float)>)" (?print@Renderer@@QEAAXV?$function@$$A6AXM@Z@std@@@Z) referenced in function main
1>D:\work\VSsource\repos\cpplearn\Project1\x64\Debug\Project1.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 22:10 and took 00.793 seconds ==========

想要实现在结构体中仅声明方法,在结构体外进行定义,但是遇到连接错误。原因是下面定义的print并没有正确地定义给结构体中的print方法。而在main()函数中却使用了他。从而出现连接报错。正确修改应该是 : 在结构体外(外命名空间中)定义方法时一定要表明类对象:修改如下:

struct Renderer
{vector<function<void(vector<string>)>> fileDropListeners;// 定义一个方法,它是将一个函数作为输入,callback是形参void print(function<void(float)> callback_func);void onFileDrop(function<void(vector<string>)> callback) {fileDropListeners.push_back(callback);}
};void Renderer::print(function<void(float)> callback_func) {float a;std::cin >> a;callback_func(a);
}void lambda(float a) {printf("lambda hello %f!", a);
}

即可


http://www.mrgr.cn/news/43441.html

相关文章:

  • 《CUDA编程》4.CUDA程序的错误检测
  • LeetCode hot100---二叉树专题(C++语言)
  • 十二、血条UI
  • 0-1背包问题
  • Windows 11 24H2 v26100.1742 官方简体中文版
  • 【图论】树剖(上):重链剖分
  • ChatGPT Canvas:交互式对话编辑器
  • Matlab编程示例24:freexyn在b站的读取手写体mnist数据集的matlab代码
  • [NeurIPS 2022] STaR: Bootstrapping Reasoning With Reasoning
  • 计算机视觉算法知识详解(含代码示例)
  • Koa2项目实战1(项目搭建)
  • 【Mybatis篇】Mybatis的关联映射详细代码带练 (多对多查询、Mybatis缓存机制)
  • C语言自定义类型联合和枚举(25)
  • Vue之父尤雨溪成立VoidZero公告,已获得 460 万美元种子轮融资
  • 【hot100-java】【将有序数组转换为二叉搜索树】
  • 事业群 BG、业务单元 BU 极简理解
  • 【C++ STL】手撕vector,深入理解vector的底层
  • AES加密算法的详细描述和C语言实现
  • 职场中的10个“人情世故”,随处可见
  • JavaWeb的小结02