-4

この場所にエラーがあります:

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に送り返すために彼をフォーマットする方法がわかりません。

4

2 に答える 2

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. すべての境界チェック関数と同様に、 が実装によって定義されている場合、およびユーザー が を含める前に整数定数 1 を 定義しているstrcpy_s場合にのみ、 が使用可能であることが保証されます。__STDC_LIB_EXT1____STDC_WANT_LIB_EXT1__string.h

詳細については、ページを参照してくださいhttp://en.cppreference.com/w/c/string/byte/strcpy

于 2017-01-02T18:01:04.500 に答える
1

最も簡単な解決策はmsgToGraphicsstd::stringtoo を作成し、C 関数を使用する代わりに、それにstrcpy_s割り当てて同じことを行うことです。

msgToGraphics = game.board_now();

基になる配列に非定数を取得する必要がある場合はchar*、次のようにすることができます (通常の注意事項があります)。

p.sendMessageToGraphics(&msgToGraphics[0]);

しかし実際には、渡される char 配列に依存しないようにインターフェイスを変更する必要があります (ヒント:std::string代わりに使用してください)。

于 2017-01-02T17:42:02.850 に答える