小编arc*_*pus的帖子

将std :: unique_ptr与自定义自由操作结合使用

我有一个带有构造函数和析构函数的类,以及以下代码:

class propagation_module{
    private:
                gsl_interp_accel *xa;
                interp2d_spline *interp_s;
    public:
        propagation_module()
        {
            std::vector<int> p_N = {1, 2, 3}, z_vec = {1, 2, 3};
            xa = gsl_interp_accel_alloc();
            interp_s = interp2d_spline_alloc(interp2d_bicubic, p_N.size(), z_vec.size());
        }
        ~propagation_module(){
            gsl_interp_accel_free(xa);
            interp2d_spline_free(interp_s);
        }
}
Run Code Online (Sandbox Code Playgroud)

我想用std :: unique_ptr-variables替换指针,但我不明白如何实现自由函数.根据其他一些问题,以下方式应该有效:

class propagation_module
{
    private: 
        std::unique_ptr<gls_interp_accel, decltype(&gsl_interp_accel_free)> xa;
        std::unique_ptr<interp2d_spline, decltype(&interp2d_spline_free)> interp_s;
    public:
        propagation_module()
        {
            //Same as above
        }
        //No destructor necessary
}
Run Code Online (Sandbox Code Playgroud)

这是正确的,还是我忘记了什么?

c++ unique-ptr c++11

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

在matlab中将函数与边界相结合

我有这样的功能:

f(x) = {  x     if 0 < x < n
       {  n-x   if n < x < 2*n
Run Code Online (Sandbox Code Playgroud)

如何在MATLAB中输入此功能?

matlab

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

即使使用其他线程,也启动while循环冻结程序

在我的(Qt-)程序中,我需要一个从外部源获得的值的连续请求.但我不希望这个请求冻结整个程序,所以我为这个函数创建了一个单独的线程.但即使它在一个单独的线程中运行,GUI也会冻结.为什么?

请求函数的代码:

void DPC::run()
{
    int counts = 0, old_counts = 0;
    while(1)
    {
        usleep(50000);
        counts = Read_DPC();
        if(counts != old_counts)
        {
            emit currentCount(counts);
            old_counts = counts;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Read_DPC()返回我想要发送到GUI中的lineEdit的int值.
主要类看起来像

class DPC: public QThread
{
    Q_OBJECT
public:
    void run();
signals:
    void currentCount(int);
};
Run Code Online (Sandbox Code Playgroud)

此代码在main函数中调用为:

DPC *newDPC = new DPC;
connect(newDPC, SIGNAL(currentCount(int)), SLOT(oncurrentCount(int)));
connect(newDPC, SIGNAL(finished()), newDPC, SLOT(deleteLater()));
newDPC->run();
Run Code Online (Sandbox Code Playgroud)

如何防止此代码冻结我的GUI?我究竟做错了什么?谢谢!

c++ qt multithreading

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

在 CMakeLists 中组合 C++ 和 CUDA 时将 CXX 标准设置为 c++17

根据CMake的文档,我只需要写

project(${PROJECT_NAME} LANGUAGES CUDA CXX)
Run Code Online (Sandbox Code Playgroud)

当我想在一个项目中结合 CUDA 文件和本机 C++ 文件时。然后我不必再打电话cuda_add_executable()了,而是add_executable,CMake 应该自己解决所有问题。这很好用,除非我想为 C++ 代码指定一个标准(通过使用set(CMAKE_CXX_STANDARD 17))。然后我收到错误消息

Target requires the language dialect "CUDA17" (with compiler extensions), but CMake does not know the compile flags to use to enable it
Run Code Online (Sandbox Code Playgroud)

是否有替代解决方案,或者我应该回到find_package(CUDA)cuda_add_executable

c++ cuda cmake

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

重载+和=时出错

我想为我的矩阵类重载运算符+和=.我的代码是:

    friend float* operator+(const Matrix& m)
    {
        for(int i = 0; i < (int)(m._n*m._n); i++)
            _m[i] = _m[i] + m._m[i];
    };

    friend Matrix& operator=(const Matrix& m)
    {
        std::swap(_m, m._m);
        return *this;
    };
Run Code Online (Sandbox Code Playgroud)

_m数据和_n方阵的大小.但是我的编译器给了我以下错误:

main.cpp:161:45: error: ‘Matrix& operator=(const Matrix&)’ must be a nonstatic member function
main.cpp: In function ‘float* operator+(const Matrix&)’:
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:13: error: from this location
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:21: error: from …
Run Code Online (Sandbox Code Playgroud)

c++ overloading

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

标签 统计

c++ ×4

c++11 ×1

cmake ×1

cuda ×1

matlab ×1

multithreading ×1

overloading ×1

qt ×1

unique-ptr ×1