【问题标题】:C++ strcpy_s IntelliSense errorC++ strcpy_s IntelliSense 错误
【发布时间】:2017-01-02 17:28:24
【问题描述】:

我在这个地方有错误:

strcpy_s(msgToGraphics, game.board_now());

错误是:

IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char [1024], std::string)    

这里是 game.board_now 函数:

string Board::board_now()
{
return _board;
}

这是我尝试使用strncpy_s()的其余代码:

#include "Pipe.h"
#include "Board.h"
#include <iostream>
#include <thread>

using namespace std;
void main()
{
    srand(time_t(NULL));

    Pipe p;
    bool isConnect = p.connect();

    string ans;
    while (!isConnect) {
        cout << "cant connect to graphics" << endl;
        cout << "Do you try to connect again or exit? (0-try again, 1-exit)" << endl;
        cin >> ans;

        if (ans == "0") {
            cout << "trying connect again.." << endl;
            Sleep(5000);
            isConnect = p.connect();
        }
        else {
            p.close();
            return;
        }
    }

    char msgToGraphics[1024];
    // msgToGraphics should contain the board string accord the protocol
    // YOUR CODE
    Board game;
    //strcpy_s(msgToGraphics, game.board_now()); // just example...

    p.sendMessageToGraphics("rnbkqbnrpppppppp################################PPPPPPPPRBNKQNBR0"); // send the board string

    // get message from graphics
    string msgFromGraphics = p.getMessageFromGraphics();

    while (msgFromGraphics != "quit") {
        game.change_board(msgFromGraphics);
        game.change_board_sq(msgFromGraphics);
        strcpy_s(msgToGraphics, game.board_now()); // msgToGraphics should contain the result of the operation

        // return result to graphics
        p.sendMessageToGraphics(msgToGraphics);

        // get message from graphics
        msgFromGraphics = p.getMessageFromGraphics();
    }

    p.close();
}

代码基本上是一个国际象棋程序,我尝试在我所做的更改后接收棋盘,但我不知道如何在strcpy_s() 中格式化他,以便将他放入数组并将其发送回给定的exe。

【问题讨论】:

  • 为什么msgToGraphics 不是std::string 呢?避免使用普通的char 数组可以避免很多麻烦,在这种情况下,您可以通过运算符 = 将一个字符串分配给另一个字符串,就像在msgToGraphics = game.board_now(); 中一样。
  • 因为它必须是数组,因为这是接口接收的内容
  • 这不是一个好的理由。您可以使用 string::c_str() 将字符指针传递给您的 API。

标签: c++ intellisense tr24731


【解决方案1】:

由于 C11 strcpy_s 是

1) `char *strcpy( char *dest, const char *src );`  
2) errno_t strcpy_s(char *restrict dest, rsize_t destsz, const char *restrict src);

strcpy_s 与 (1) 相同, 除了它可能会用未指定的值破坏目标数组的其余部分,并且在运行时检测到以下错误并调用当前安装的约束处理函数:

  • src 或 dest 是 null 指针
  • destsz 为零或大于 RSIZE_MAX
  • destsz 小于或等于strnlen_s(src, destsz);,也就是说会发生截断
  • 重叠会在源字符串和目标字符串之间发生

    1. 如果 dest strnlen_s(src, destsz) < destsz; 指向的字符数组的大小,即 destsz 的错误值不会暴露即将发生的缓冲区溢出,则行为未定义。

    2. 作为所有边界检查函数,只有在实现定义了 __STDC_LIB_EXT1__ 并且用户在包含 string.h 之前将 __STDC_WANT_LIB_EXT1__ 定义为整数常量 1 时,才能保证 strcpy_s 可用。

更多信息请参考页面http://en.cppreference.com/w/c/string/byte/strcpy

【讨论】:

    【解决方案2】:

    最简单的解决方案是将msgToGraphics 也设为std::string,然后不使用C 函数strcpy_s,只需分配给它来做同样的事情:

    msgToGraphics = game.board_now();
    

    如果您需要为底层数组获取一个非常量 char*,您可以这样做(带有通常的警告):

    p.sendMessageToGraphics(&msgToGraphics[0]);
    

    但实际上,您应该更改接口以不依赖传入的 char 数组。(提示:改用 std::string。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2021-02-08
      相关资源
      最近更新 更多