当我使用的教程完全没有错误时,为什么我的代码错误
Why does my code error when the tutorial I used does not have an error at all?
我按照字符遵循此教程字符,但由于某种原因,我会收到以下错误:
" 1> c: users fish的海洋 source repos repos dll1 dll1 dll1 dll1.cpp(47):错误c2664:'bool(ds_map,char *,double)':无法从''转换参数2const char [5]'to'char *'"
也在第48和49行。
这是我的视觉工作室在这些行上的样子,但是出于某种原因,教程没有错误,我无法弄清楚差异(他在17分钟大约17分钟左右的位置上遍及这些行)
// Dll1.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <thread>
#include <chrono>
#include <vector>
#include <mutex>
#define gmx extern "C" __declspec(dllexport)
using ds_map = int;
using thread = std::thread;
using milliseconds = std::chrono::milliseconds;
const int EVENT_OTHER_SOCIAL = 70;
void(*gml_event_perform_async)(ds_map map, int event_type) = nullptr;
int(*gml_ds_map_create)(int n, ...) = nullptr;
bool(*gml_ds_map_add_double)(ds_map map, char* key, double value);
bool(*gml_ds_map_add_string)(ds_map map, char* key, char* value);
std::mutex thread_key;
std::vector<thread*> threads;
std::vector<uint32_t> open_slots;
gmx double RegisterCallbacks(char * arg1, char* arg2, char* arg3, char* arg4) {
gml_event_perform_async = (void(*)(ds_map, int))arg1;
gml_ds_map_create = (int(*)(int, ...))arg2;
gml_ds_map_add_double = (bool(*)(ds_map, char*, double))arg3;
gml_ds_map_add_string = (bool(*)(ds_map, char*, char*))arg4;
return 0;
}
ds_map ds_map_create() {
return gml_ds_map_create(0);
}
void return_double(double time, double type, double value, int handle) {
long t = (long)time;
std::this_thread::sleep_for(milliseconds(t));
thread_key.lock();
ds_map map = ds_map_create();
gml_ds_map_add_double(map, "type", type);
gml_ds_map_add_double(map, "value", value);
gml_ds_map_add_double(map, "handle", handle);
gml_event_perform_async(map, EVENT_OTHER_SOCIAL);
thread_key.lock();
}
gmx double thread_create(double time, double type, double value) {
int index;
thread_key.lock();
if (open_slots.empty()) {
index = threads.size();
threads.push_back(new thread(return_double, time, type, value, index));
}
else {
index = open_slots.back();
open_slots.pop_back();
threads[index] = new thread(return_double, time, type, value, index);
}
thread_key.unlock();
return index;
}
gmx double thread_kill(double index) {
thread_key.lock();
if (threads.size() > index && threads[index] != NULL) {
if (threads[index]->joinable()) {
threads[index]->detach();
}
delete threads[index];
threads[index] = NULL;
open_slots.push_back(index);
}
thread_key.unlock();
return 1;
}
gmx double thread_free(double index) {
thread_key.lock();
if (threads.size() > index && threads[index] != NULL) {
if (threads[index]->joinable()) {
threads[index]->join();
}
delete threads[index];
threads[index] = NULL;
open_slots.push_back(index);
}
thread_key.unlock();
return 1;
}
看来您的教程很古老。早在很久以前,一些编译器将允许将字符串-"type"
传递给想要char *
的参数,或将这些值分配给char *
变量。(字符串不被视为常数。)
这是更改的,使得字符串是恒定的(仅读取)。这意味着您的char *
参数中的大多数(如果不是全部)都需要更改为const char *
,以便与您使用的字符串一起使用。但是,这意味着被用作回调的功能也需要更改其签名。
和 RegisterCallbacks
应为所有参数采用正确的函数原型,而不接受它们为 char *
值。这些天,实现这一目标所需的演员很容易导致不确定的行为。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 为什么我的for循环不能正确获取argv
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的 std::ref 无法按预期工作?
- 为什么我的删除节点函数实际上没有删除节点?
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 为什么我的多线程作业队列崩溃
- 为什么我的排序算法会更改数组值
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 为什么我的共享库中存在展开符号
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 为什么我的好友类无法访问私人会员?
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么我的模板化函数需要从一个迭代器转换到另一个迭代器?