相关疑难解决方法(0)

如何将void*转换为函数指针?

我在FreeRTOS中使用xTaskCreate,其第四个参数(void*const)是传递给新线程调用的函数的参数.

void __connect_to_foo(void * const task_params) {
   void (*on_connected)(void);
   on_connected = (void) (*task_params);
   on_connected();
}

void connect_to_foo(void (*on_connected)(void)) {
   // Start thread
   xTaskCreate(
         &__connect_to_foo,
         "ConnectTask",
         STACK_SIZE,
         (void*) on_connected, // params
         TASK_PRIORITY,
         NULL // Handle to the created Task - we don't need it.
         );
}
Run Code Online (Sandbox Code Playgroud)

我需要能够传入一个带签名的函数指针

void bar();

但我无法弄清楚如何将void*转换为我可以调用的函数指针.我能得到的最近的是:

错误:'void*'不是第3行的指针对象类型

如何将task_params转换为我可以调用的函数指针?

注意,上面的代码大大简化了.

c++ function-pointers freertos

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

C++拷贝对象

我有一个Game包含其他对象的类GameGrid:

Game.h

class Game
{
        public:
            Game(uint32_t GridSize);
        private:
            GameGrid _CurrentGrid;
}
Run Code Online (Sandbox Code Playgroud)

Game.cpp

Game::Game(uint32_t GridSize) : _CurrentGrid(GridSize)
{
}
Run Code Online (Sandbox Code Playgroud)

GameGrid.h

class GameGrid
{       
        public:
            GameGrid(uint32_t GridSize);
            void setGrid(const Grid& Grid);
            const GameGrid::Grid getGrid(void) const;
        private:
            Grid _Grid;
}
Run Code Online (Sandbox Code Playgroud)

GameGrid.cpp

GameGrid::GameGrid(uint32_t GridSize)
{
    uint32_t i = 0;
    uint32_t j = 0;

    _Grid.assign(GridSize, std::vector<unsigned int>(GridSize));

    for (i = 0; i < GridSize; i++)
    {
        for (j = 0; j < GridSize; j++)
        {
            _Grid.at(i).at(j) = 0;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c++

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

当使用'extern"C"'时,是什么导致错误"预期字符串文字,但发现用户定义的字符串文字"?

我有包含这些声明的代码:

class IRealNetDll;

extern "C"__declspec(dllexport)
IRealNetDll * CreateRealInstance(int ilicence);
Run Code Online (Sandbox Code Playgroud)

这在Win7上使用Visual Studio 2012正确构建.

但是对于Windows 10上的VS 2015,2017,这一行:

extern "C"__declspec(dllexport)
Run Code Online (Sandbox Code Playgroud)

结果是:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C3690: expected a string literal, but found a user-defined string literal instead
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error: missing ';' before identifier 'IRealNetDll'
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误,为什么只有较新的编译器,以及如何防止它?

c++ visual-studio visual-c++ visual-studio-2012 windows-10

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

模板类型后的星号'*'是什么意思?

我来了一个模板代码,用于*模板类型之后:

  ...
  template <typename _Up, typename _Ep, typename = void>
  struct _Ptr
  {
      using type = _Up*; 
  };
  ...
Run Code Online (Sandbox Code Playgroud)

我看到的另一种用法

   ...
   template <typename T, typename \
   std::enable_if<std::is_integral<T>::value,T>::type* = nullptr>
   ...
Run Code Online (Sandbox Code Playgroud)

这个结构描述在哪里?

c++ templates

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

C ++ std :: vector条目的功能为NULL,但大小保持在零以上

我用C / C ++编写代码已经有一段时间了,我已经找到解决问题的替代方法,但是我想知道为什么原始代码不起作用。

我有一个测试类,基本上只存储一个字符串。

class test {
private:
        std::string name;
public:
        test(std::string name) : name(name) {};
        std::string get_name() { return name; }
};
Run Code Online (Sandbox Code Playgroud)

在其中,main我有一个向量,我有时会用test对象填充。下面的代码模拟了vector的不规则用法vect

int main(void) {
        std::vector<test *> vect;
        std::vector<test *>::iterator i;

        //* Comment this for a working example
        std::cout << "Searching empty vector" << std::endl;
        i = *(_is_in_vector(vect, std::string("test 3")));
        if (i == vect.end()) {
                std::cout << "Nothing found" << std::endl;
        } // */

        vect.push_back(new test("test 1"));
        vect.push_back(new test("test 2"));
        vect.push_back(new …
Run Code Online (Sandbox Code Playgroud)

c++ iterator vector segmentation-fault

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

使用&符号命名的变量后跟下划线是什么意思?

我正在研究C ++,发现了这种奇怪的语法。这&_是什么意思,为什么作者可以将该变量用作_1st

std::sort(open_list.begin(), open_list.end(), [] (const auto &_1st, const auto &_2st) {
  return _1st->h_value + _1st->g_value < _2st->h_value + _2st->g_value
});
Run Code Online (Sandbox Code Playgroud)

编辑1

我认为,要创建变量的引用,必须使用&运算符,后跟名称,例如:&_1st(注意它们之间的空白)。

下划线是在C ++中声明名称变量的一种特殊方法吗?

c++ reference c++17

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

这本书的第252页上有错字“ c ++的完整指南”?

我正在读《 c ++的完整指南》这本书。我认为第252页有错字。因此,我有以下三个文件。

在文件account.h中,

// account.h
// Defining the class Account.     class definition (methods prototypes) is usually put in the header file
// ---------------------------------------------------
#ifndef _ACCOUNT_                                             // if _ACCOUNT_ is not defined
#define _ACCOUNT_

#include <iostream>
#include <string>
using namespace std;


class Account
{
   private:
     string name;
     unsigned long nr;
     double balance;

   public:            //Public interface:
     bool init( const string&, unsigned long, double);
     void display();
};
#endif
// _ACCOUNT_
Run Code Online (Sandbox Code Playgroud)

在account.cpp文件中,

// account.cpp
// Defines methods init() and display().
// ---------------------------------------------------
#include …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors

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

无效的整型常量表达式

在这段代码中:

// CompileTimeWarnings.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <type_traits>
using namespace std;
#define __STR1__(x) #x

#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : warning : "

// collisions.cpp

template<class T, class T1>
struct mismatch
{
    //enum {value = is_signed<T>::value && is_signed<T1>::value};
    static const bool value; //= is_signed<T>::value && is_signed<T1>::value;
};

template<class T, class T1>
bool mismatch<T,T1>::value = is_signed<T>::value && is_signed<T1>::value;

template<class T>
struct Int
{
};

template<class T, class T1>
int operator+(Int<T> t, Int<T1> t1) …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio-2010

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

Windows系统编程OpenFile功能

Windows中的OpenFile功能文档位于此处.而我正在尝试这样做:

#include "Tchar.h"
#include <windows.h>
int main(int argc, TCHAR *argv[]){

    LPOFSTRUCT _buffer;
    HFILE _hfile_ = OpenFile("E:\\mozunit.txt", _buffer, OF_READ);
    LPVOID _buffer_read;
    LPDWORD _bytes_read;
    bool flag = ReadFile(_buffer, _buffer_read, 5, _bytes_read, NULL);
    CloseHandle(_buffer);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行这个时,我得到一个我没有初始化的错误_buffer.所以要反击我这样初始化_buffer:

LPOFSTRUCT _buffer = NULL;
Run Code Online (Sandbox Code Playgroud)

这给了我一个访问冲突错误.这是为什么?

c++ windows

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

错误C2678:二进制'==':找不到带左手操作数的运算符

我无法弄清楚为什么我得到这个错误但是同一个类在VS15下完美运行现在我正在使用VS12,它是一个简单的Winsock2实现,

int Net::createServer(int port, int protocol) 
{
    int status;

    // ----- Initialize network stuff -----
    status = initialize(port, protocol);
    if (status != NET_OK)
        return status;

    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);    // listen on all addresses

    // bind socket
    if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
    {
        status = WSAGetLastError();          // get detailed error
        return ((status << 16) + NET_BIND_FAILED);
    }
    bound = true;
    mode = SERVER;

    return NET_OK;
}
Run Code Online (Sandbox Code Playgroud)

问题来自这里

if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
Run Code Online (Sandbox Code Playgroud)

控制台日志:

error C2678: binary '==' …
Run Code Online (Sandbox Code Playgroud)

c++ operators winsock2

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