【发布时间】: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