简体   繁体   English

C++ 文字和常量之间的连接

[英]C++ concatenation between literal and constant

I am new to C++ and I want to know why the line below is not correct.我是 C++ 的新手,我想知道为什么下面的行不正确。 (SERVER_NAME has been defined as a constant) (SERVER_NAME 已定义为常量)

L"Initial Catalog=DatabaseName;Data Source=" << SERVER_NAME << ";"

I get these errors:我收到这些错误:

error C2296: '<<' : illegal, left operand has type 'const wchar_t [...' 
error C2308: concatenating mismatched strings

Thanks谢谢

operator<< is not a concatenation operator, it's a special overloaded operator by stream types to let you send data to stream. operator<< 不是连接运算符,它是 stream 类型的特殊重载运算符,可让您将数据发送到 stream。 It only works if you're using a stream.它仅在您使用 stream 时才有效。

You have two options here.您在这里有两个选择。 First, you can use a std::wstring:首先,您可以使用 std::wstring:

std::wstring(L"Initial Catalog=DatabaseName;Data Source=") + SERVER_NAME + L";";

Or you can use a wstringstream (from the <sstream> header):或者您可以使用 wstringstream(来自<sstream>标头):

std::wstringstream stream;
stream << L"Initial Catalog=DatabaseName;Data Source=" << SERVER_NAME << L";"

Use stream.str() to get the resulting string in that case.在这种情况下,使用stream.str()获取结果字符串。 The stream approach has the advantage that you can use it even if not all the things you want to concatenate are already strings. stream 方法的优点是即使不是所有要连接的东西都已经是字符串,也可以使用它。

If you're printing to an existing stream (like wcout), you can just skip the stringstream and use that directly, of course.如果您要打印到现有的 stream(如 wcout),您当然可以跳过字符串流并直接使用它。

As other answers have pointed out, you can use L"Initial Catalog=DatabaseName;Data Source=" SERVER_NAME L";"正如其他答案所指出的,您可以使用L"Initial Catalog=DatabaseName;Data Source=" SERVER_NAME L";" if SERVER_NAME is a constant create with #define.如果 SERVER_NAME 是使用#define 创建的常量。 If it is a const wchar_t* that won't work however.但是,如果它是一个const wchar_t*则不起作用。

<< and >> are not concatenation operators, but rather bitwise shifting operators, something that is totally unrelated. <<>>不是连接运算符,而是按位移位运算符,完全不相关。

It's confusing for beginners because they have been overloaded for cout and cin to mean something completely different.这对初学者来说很困惑,因为它们已经被coutcin重载以表示完全不同的东西。

Concatenation of string literals in C and C++ is done without a special operator, so just type: C 和 C++ 中字符串文字的连接无需特殊运算符,因此只需键入:

L"Initial Catalog=DatabaseName;Data Source=" SERVER_NAME L";"

It may be that SERVER_NAME is not a wide-character string. SERVER_NAME可能不是宽字符串。 However, what's definately broken is that your ";"但是,绝对有问题的是您的";" is not a wide-character string.不是宽字符串。 Try using L";"尝试使用L";"

If you want to concatenate this string use std::stringstream如果要连接此字符串,请使用 std::stringstream

std::stringstream ss;
ss << "Initial Catalog=DatabaseName;Data Source=" << SERVER_NAME << ";";
std::string final = ss.str();
std::wstring connectionString = "Initial Catalog=DatabaseName;Data Source=";
connectionString += SERVER_NAME + ";";

Make sure SERVER_NAME is defined as wide-character string, something like this:确保将SERVER_NAME定义为宽字符串,如下所示:

const wchar_t *SERVER_NAME = L"testdb";

Or,或者,

std::wstring SERVER_NAME = L"testdb";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM