C++如何正确打印? 我正在用C++编程一个小游戏,在打印板时遇到问题。

C++如何正确打印? 我正在用C++编程一个小游戏,在打印板时遇到问题。,c++,string,pointers,tic-tac-toe,C++,String,Pointers,Tic Tac Toe,以下是代码语法。h是一个头文件,具有诸如print、println、input等函数: #include "syntax.h" // contains helpful functions such as "print" and "println" to shorten code char board[3][3]; void print_board(); int main() { print_board(); } void print_board() { for (int i

以下是代码语法。h是一个头文件,具有诸如print、println、input等函数:

#include "syntax.h" // contains helpful functions such as "print" and "println" to shorten code

char board[3][3];
void print_board();

int main()
{
    print_board();
}
void print_board()
{
    for (int i = 0; i < 3; i++)
    {
        println("-------");
        for (int j = 0; j < 3; j++)
        {
            print("|" + board[i][j] + " "); // ERROR - Cannot add two pointers
        }
        println("|");
    }
    println("-------");
    input();
}
print是syntax.h中的一个函数,它接收字符串变量并用cout打印,然后刷新输出缓冲区

现在,我不能打印上面这样的字符串,因为它告诉我不能添加两个指针

我理解为什么会发生这种情况,这是因为打印中的参数实际上是char*而不是字符串变量,我无法将它们相加

问题是,我也不想再进行另一次打印函数调用,而是在同一个函数调用中打印所有这3个字符串

那么,我应该如何在没有错误的情况下打印上面的内容呢?

使用sprintf函数:

//print("|" + board[i][j] + " "); // ERROR - Cannot add two pointers 
char buffer[100];   
sprintf(buffer, "| %s ", board[i][j]);
print(buffer); 
如果要使用字符串类型,可以执行以下操作:

//print("|" + board[i][j] + " "); // ERROR - Cannot add two pointers    
print(string("|") + string(board[i][j]) + string(" "));  
而不是

print("|" + board[i][j] + " ");
试一试

string有一个重载运算符+用于连接。别忘了

#include <string>

好的,但这不是C的函数吗?我不能使用C++打印函数,只使用这样的打印字符串,+符号将它们添加到打印字符串参数中吗?SpReFTF是C和C++的标准函数。它在头文件中声明。如果您提供了打印的实现,我可以尝试为您更改它。如果我使用printstring |+board[I][j]+string;,但是每次打印都要写太多了。我的实现非常简单:print函数得到一个字符串,然后用cout打印它。我只是不喜欢你必须使用我的新的实现,使用字符串类型PayPoto我的无知,但我没有看到打印或打印在C++中。这些是你的职能吗?是的。请下次阅读整个问题,因为我已经写了太多次了,它们是我的函数。
#include <string>