小编Fat*_*sis的帖子

插入成员函数指针的映射

我的问题有点复杂,所以我将从一个例子开始:

class a
{
public:
     a()
     {
          pointerMap.insert(pair<std::string, void a::*(int, int)> ("func1", func1);
          pointerMap.insert(pair<std::string, void a::*(int, int)> ("func2", func2);
     }

private:
     void func1(int a, int b);
     void func2(int a, int b);
     std::map<std::string, void a::* (int, int)> pointerMap;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,这是将成员函数的指针添加到对象中的映射的正确方法,以便您只引用单个实例func1func2

而且,我不知道如何从指针调用此函数.它会是这样的吗?

map["func1"](2,4);
Run Code Online (Sandbox Code Playgroud)

在使用成员函数时,我对语法有点困惑.

c++

3
推荐指数
1
解决办法
3490
查看次数

在HashMap中存储闭包

为了学习Rust语言,我正在使用一个旧的C++库,我已经躺在那里并尝试将其转换为Rust.它使用了很多C++ 11闭包,我在翻译概念方面遇到了一些困难.

在C++中我有这样的事情:

// library.h
struct Event {
    // just some data
};

class Object {
public:
    // ...
    std::function<void(Event&)>& makeFunc(std::string& s) {
        return m_funcs[s];
    }
    // ...
private:
    // ...
    std::map<std::string, std::function<void(Event&)>> m_funcs;
    // ...
};

// main.cpp using the library
int main()
{
    Object foo;
    foo.makeFunc("func1") = [&]{
        // do stuff
    };
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我遇到问题的部分是将函数正确存储在Rust HashMap集合中.我试过这个:

struct Event;

struct Object {
    m_funcs : HashMap<String, FnMut(&Event)>
}

impl Object {
    // send f as another parameter rather than …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
2063
查看次数

未收到Poco :: Net :: HTTPClientSession json数据Content-Type

因此,我正在开发服务器端Nodejs / expressjs应用程序和客户端c ++ / Poco应用程序。我设法在托管服务器和客户端之间创建会话。但是,无论何时我尝试发送JSON有效负载,express.js都会将req.body显示为空。

Google并没有透露太多内容,除了Content-Type可能无法正确传输之外,看起来也是如此。我确实对其进行了明确设置,但显然我错过了一步。

客户端

void upload(std::list<std::string>& args) {
    if (args.size() == 0 || args.front() == "--help") {
        help("upload");
        return;
    }

    std::string repo = args.front();
    args.pop_front();

    std::string name, language;
    auto depends = getDepends(name, language);

    // start making the poco json object here
    Poco::JSON::Object obj;
    obj.set("name", name);
    obj.set("url", repo);

    Poco::URI uri("http://url-of-my-server:50001/make_repo");
    std::string path(uri.getPathAndQuery());


    if (path.empty()) path = "/";

    HTTPClientSession session(uri.getHost(), uri.getPort());
    HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
    HTTPResponse response;

    std::ostream& o = session.sendRequest(request);

    std::cout << response.getStatus() << " " << …
Run Code Online (Sandbox Code Playgroud)

c++ json http node.js poco-libraries

3
推荐指数
1
解决办法
2988
查看次数

GLEW链接问题找不到__imp_glGetIntegerv

注意:是的,我指定了GLEW_STATIC。

因此,在尝试设置在新项目中使用的glew之前,我已经经历过这种牛仔竞技表演,但是现在我在项目中使用glew 2.0,它会产生链接错误。我刚刚在Linux实例中生成了源代码,并像这样使用它们。

#include "Renderer.h"

#include <windows.h>
#include "GL/glew.h"
#include "Logger.h"

void Renderer::init(void* windowHandle) {
    Logger logger("Renderer::init");

    GLenum result = glewInit();
    if (result != GLEW_OK) {
        LOG(logger) << "Failed to run glew init with error: " << result;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我在名称空间渲染器中声明并在此处定义的函数。与此相关的是,我仅致电glewInit(),就是这样。

产生的链接错误是:

Error   LNK2019 unresolved external symbol __imp_glGetIntegerv referenced in function glewContextInit
Error   LNK2019 unresolved external symbol __imp_glGetString referenced in function glewContextInit
Error   LNK2019 unresolved external symbol __imp_wglGetCurrentDC referenced in function wglewInit
Error   LNK2019 unresolved external symbol __imp_wglGetProcAddress …
Run Code Online (Sandbox Code Playgroud)

c++ windows opengl glew visual-studio-2015

3
推荐指数
1
解决办法
1855
查看次数

是否可以在类中拥有虚拟类声明?

我正在为个人项目中的框架的各种组件设置一个接口,我突然想到了一些我认为可能对接口有用的东西.我的问题是这是否可能:

class a
{
public:
    virtual class test = 0;

};

class b : public a
{
public:
    class test
    {
        public:
           int imember;
    };
};

class c : public a
{
public:
    class test
    {
    public:
           char cmember;  // just a different version of the class. within this class
    };
};
Run Code Online (Sandbox Code Playgroud)

声明需要在派生对象中定义的虚拟类或纯虚拟类,以便您可以执行以下操作:

int main()
{
    a * meh = new b();
    a * teh = new c();

    /* these would be two different objects, but have the same name, …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
119
查看次数

D3D11无处不在增加了参考数量?

我一直在使用d3d11已经有一段时间了,在发现了directx调试器后,我最近发现我的程序从所有未正确释放的com对象泄漏内存.经过一段时间的窥探和几个小时的盯着代码,我已经开发了一些方法来隔离我在这里获得这些意外增加的引用计数.

首先,所有对象都包含在std :: shared_ptrs中,并带有调用各自释放功能的自定义删除器.我这样做了所以我永远不必调用addref,并且只有当对象超出范围时才会调用第一次调用release(删除器中的那个).它看起来像这样:

// in D3D11Renderer.h
...
// declaration
std::shared_ptr<ID3D11Device *> m_Device;
...

// after call to ID3D11CreateDeviceAndSwapChain
m_Device.reset(device, [](ID3D11Device * ptr){ptr->Release();})
Run Code Online (Sandbox Code Playgroud)

问题是api调用中的某些随机函数会随机增加引用计数,期望我以后必须处理它.

我发现在诊断中有用的东西是一个看起来像这样的函数:

template <typename T>
int getRefCount(T object) 
{
    object->AddRef();
    return object->Release();
}
Run Code Online (Sandbox Code Playgroud)

只是增加和减少计数以获得该对象的当前refs数.使用这个,我发现,在调用自定义删除器的发布之前,有10个对我创建的ID3D11Device的未完成引用.好奇,我慢慢地回溯,在程序中一直调用这个函数,直到我最初创建它的地方.有趣的是,在我第一次创建对象之后(甚至在shared_ptr获得所有权之前),未完成的引用数已经是3!这之后立即发生这种情况.

result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, 
                           D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, NULL, &deviceContext);
    if(FAILED(result))
    {
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

这是我第一次调用创建设备的任何此类函数,当我检查后面有多少引用时,它说3!很明显,我误解了这些com对象应该被处理的方式.有没有这样的方式,我可以手动删除它们,而不是在那里使用幕后引用计数废话?

c++ com directx direct3d11 c++11

2
推荐指数
1
解决办法
2219
查看次数

Dlang:如何在模板函数上强制执行接口

我一直在谷歌搜索两个小时,没有运气.

如果我有模板功能,并且我想在模板类型上强制实施接口,我该怎么办?

恩.

void doStuff(T)(bool param) /*if T is a Throwable*/ {
    // do stuff...
}
Run Code Online (Sandbox Code Playgroud)

d

2
推荐指数
1
解决办法
149
查看次数

Angular 2中的Fetch.js抱怨CORS请求

我模糊地按照快速入门教程设置并在与expressjs后端通信的应用程序中使用angular 2.我已经解决了一些依赖问题并解决了大部分依赖问题,但我陷入困境.我遇到的错误是:

Fetch API cannot load npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js. URL scheme must be "http" or "https" for CORS request.
Run Code Online (Sandbox Code Playgroud)

我的systemjs.config.js文件是他们的快速入门中指定的文字.

(function(global) {
    System.config({
        paths: {
            'npm': 'node_modules/'
        },

        map: {
            app: 'app',

            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

            // other libraries
            'rxjs':                      'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
        },

        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                defaultExtension: 'js'
            },
            rxjs: …
Run Code Online (Sandbox Code Playgroud)

javascript express angular

2
推荐指数
1
解决办法
1385
查看次数

即使注册成功,CreateWindowEx也会因错误1407而失败?

当我使用d3d时,我一直在尝试在类中包装一些基本的win32功能,并且难以理解为什么CreateWindowEx函数在我使用RegisterClassEx创建一个有效类之后失败说一个类不存在,没有错误可言: \.我觉得我错过了一些愚蠢的小事,但我找不到它.以下是一些代码:

我有一个像这样扩展WNDCLASSEX的类,所以它有一个普通的std :: string作为类名和一个简化的构造函数:

#ifndef WINDOWCLASS_H
#define WINDOWCLASS_H

#include <Windows.h>
#include <string>
#include "WindowAbstract.h"
using namespace std;

class WindowClass : public WNDCLASSEX
{
public:
    WindowClass(string className, WindowAbstract * window);
    ~WindowClass();

    bool Register();
    string ClassName() {return m_className;}

    friend class WindowAbstract;

private:
    string m_className;
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是该类的构造函数:

WindowClass::WindowClass(string className, WindowAbstract * window)
{

    cbSize        = sizeof(WNDCLASSEX);
    style         = 0;
    lpfnWndProc   = window->WndProc;
    cbClsExtra    = 0;
    cbWndExtra    = 0;
    hInstance     = hInstance;
    hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    hCursor       = LoadCursor(NULL, IDC_ARROW);
    hbrBackground = …
Run Code Online (Sandbox Code Playgroud)

c++ winapi

1
推荐指数
1
解决办法
6053
查看次数

std :: shared_ptr或std :: unique_ptr赋值运算符重载

我没有看到为什么这些没有为它们被模板化的类型的普通旧指针赋值操作符重载的原因.如果使智能指针的接口尽可能接近普通的旧指针,那么为什么它们不像这样对赋值运算符进行重载?

inline std::shared_ptr<type> &operator=( const type * pointer)
{
    reset(a);
}
Run Code Online (Sandbox Code Playgroud)

这样你可以像使用普通指针一样使用它们,如下所示:

std::shared_ptr<int> test = new int;
Run Code Online (Sandbox Code Playgroud)

它根本不是一个问题,只是想知道为什么他们遇到了只是让一些运营商超载的麻烦.

还想知道是否有一种方法来重载全局赋值运算符来执行此操作,或者是否有任何原因我不应该这样做.

编辑:在此处为Nawaz添加关于代码格式的回答.我刚刚编写了这个测试程序,看看你说的是对的:

template<class T>
class peh
{
public:
    peh() {meh = 3;}
    const peh<T> & operator=(const int * peh)
    {
    }
};

void f( peh<int> teh)
{

}

int main()
{
    int * meh = new int;

    f(meh);

    system("PAUSE");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个错误在这里说出来也从没有可用的转换peh<int>int *.那么,为什么它可以接受std::shared_ptr<int>int *

c++ smart-pointers shared-ptr unique-ptr

1
推荐指数
1
解决办法
2380
查看次数