C++ strcpy_s IntelliSense error

937 views Asked by At

I have error in this place:

strcpy_s(msgToGraphics, game.board_now());

the error is:

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

and here is the game.board_now func:

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

and here is the rest of the code where I try to use the 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();
}

The code is basically for a chess program and I try to recieve the chess board after the changes I made and I dont know how to format him in the strcpy_s() in order to put him in the array and send it back to the given exe.

2

There are 2 answers

0
Suhas More On BEST ANSWER

Since C11 strcpy_s is

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 is Same as (1), except that it may clobber the rest of the destination array with unspecified values and that the following errors are detected at runtime and call the currently installed constraint handler function:

  • src or dest is a null pointer
  • destsz is zero or greater than RSIZE_MAX
  • destsz is less or equal strnlen_s(src, destsz); in other words, truncation would occur
  • overlap would occur between the source and the destination strings

    1. The behavior is undefined if the size of the character array pointed to by dest <= strnlen_s(src, destsz) < destsz; in other words, an erroneous value of destsz does not expose the impending buffer overflow.

    2. As all bounds-checked functions, strcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.

Refer the page for more information http://en.cppreference.com/w/c/string/byte/strcpy

0
Emil Laine On

The simplest solution would be to make msgToGraphics a std::string too, and then instead of using the C function strcpy_s, just assign to it to do the same thing:

msgToGraphics = game.board_now();

If you need to get a non-const char* to the underlying array, you can do it like this (with the usual caveats):

p.sendMessageToGraphics(&msgToGraphics[0]);

But really you should change the interface to not rely on a char array being passed in. (Hint: use std::string instead.)