当我使用的教程完全没有错误时,为什么我的代码错误

Why does my code error when the tutorial I used does not have an error at all?

本文关键字:为什么 我的 错误 代码 有错误 的教程      更新时间:2023-10-16

我按照字符遵循此教程字符,但由于某种原因,我会收到以下错误:

" 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 *值。这些天,实现这一目标所需的演员很容易导致不确定的行为。