C++ 函数错误——无法将大括号括起来的初始值设定项列表转换为 char*

C++ Function error -- could not convert brace-enclosed initializer list to char*

初学C++,比较熟悉MATLAB和Arduino。我正在尝试创建(阅读:修改其他人的代码)一个 C++ 函数以通过串行发送字符数组——它与 C 库交互 (rs232.h)。在函数初始化中初始化 mode--bits/baud/parity 数组的默认值时,我一直收到此错误。不确定我是否正在尝试做一些不受支持的事情,如果是这样,我可以拆分变量。在此先感谢您的帮助。

IDE: Code::Blocks

编译器:MinGW-g++/GCC 7.3

错误:

error: could not convert '{'8', 'N', '1', 0}' from '<brace-enclosed initializer list>' to 'char*'

代码:

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include "rs232.h"
#include <string> /* Probably unnecessary */

bool Write(char (&toWrite)[256], int portNum=3, int bdrate=9600, char mode[]={'8','N','1','[=11=]'})
{
  int i, cport_nr = portNum - 1;
  if(RS232_OpenComport(cport_nr, bdrate, mode))
  {
    return false;
  }
  while(1)
  {
    RS232_cputs(cport_nr, toWrite);
    printf("sent: %s\n", toWrite);
    Sleep(1000);
    i++;
    i %= 2;
  }
  return true;
} 

将默认值放在单独的一行:

bool Write(char (&toWrite)[256], int portNum=3, int bdrate=9600, char *mode=NULL) {
    char mode_default[] = {'8','N','1','[=10=]'};
    if (mode == NULL) mode = mode_default;

原因:

You cannot use default values with C array parameters (which really decay to pointers here) –