如何串联一个const char数组和一个char数组指针? [英] How to concatenate a const char array and a char array pointer?

查看:92
本文介绍了如何串联一个const char数组和一个char数组指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接从事业务:我的代码大致如下:

Straight into business: I have code looking roughly like this:

char* assemble(int param)
{
    char* result = "Foo" << doSomething(param) << "bar";
    return result;
}

现在我得到的是:

error: invalid operands of types ‘const char [4]’ and ‘char*’ to binary ‘operator<<’

修改: doSomething返回char*.

那么,如何将这两个连接起来?

So, how do I concatenate these two?

其他信息:
编译器:GNU/Linux 2.6.32-5-amd64上的g ++ 4.4.5

Additional info:
Compiler: g++ 4.4.5 on GNU/Linux 2.6.32-5-amd64

推荐答案

"Foo""Bar"是文字,它们没有插入(<<)运算符.

"Foo" and "Bar" are literals, they don't have the insertion (<<) operator.

如果要进行基本串联,则需要使用std::string:

you instead need to use std::string if you want to do basic concatenation:

std::string assemble(int param)
{
    std::string s = "Foo";
    s += doSomething(param); //assumes doSomething returns char* or std::string
    s += "bar";
    return s;
}

这篇关于如何串联一个const char数组和一个char数组指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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