C++ 类型为“char*"和“const char [2]"的无效操作数到二进制“operator+" [英] c++ invalid operands of types 'char*' and 'const char [2]' to binary 'operator+'

查看:42
本文介绍了C++ 类型为“char*"和“const char [2]"的无效操作数到二进制“operator+"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试执行此简单代码时,编译器向我返回错误'char*' 和 'const char [2]' 类型的无效操作数到二进制 'operator+'":

the compiler return me the error "invalid operands of types 'char*' and 'const char [2]' to binary 'operator+'" when trying to do this simple code:

BodyText[client] = PS3::ReadString(0x0178646c) + "\n" ;

这里是我的 ReadString() 函数:

Here my ReadString() function:

char returnRead[100];
char* ReadString(int address)
{
    memset(&returnRead[0], 0, sizeof(returnRead));
    int strlength = 100;
    char* StrBytes = ReadBytes(address, strlength);
    for (int i = 0; i < strlength; i++)
    {
        if (StrBytes[i] != 0x00)
            returnRead[i] = StrBytes[i];
    else
        break;
}
return returnRead;
}

无论如何感谢阅读

推荐答案

这是因为 char* (函数的返回类型)没有 operator+const char[2]("\n" 的类型),并且由于您不能为内置类型重载运算符,因此不能有一个.由于这个问题被标记为 C++:

This is because there is no operator+ for char* (the return-type of your function) and const char[2] (the type of "\n"), and since you cannot overload operators for built-in types, there cannot be one. Since this question is tagged C++:

只需使用std::string 而不是char*,您的所有问题都已解决.std::string 将优于您尝试做的 hack.

Just use std::string instead of char*, all your problems are solved already. std::string will be superior to the hacks you try to do.

此处您可以找到字符串<的概述/code> 功能和示例如何使用它们.然后你可以连接字符串 a,b,cstd::string new_string = a + b + c;

这篇关于C++ 类型为“char*"和“const char [2]"的无效操作数到二进制“operator+"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆