C++ 多行字符串未按预期工作
C++ multi line string not working as expected
我正在尝试为 Windows 上的应用程序创建横幅。
// C Libraries:
#include <string>
#include <iostream>
#include <algorithm>
// Namespaces:
using namespace std;
const char *BANNER[] = R"BANNER(
.-') _ .-')
( OO) ) ( OO ).
,--. ,--. / '._ ,-.-') ,--. ,-.-') (_)---\_)
| | | | |'--...__)| |OO) | |.-') | |OO)/ _ |
| | | .-')'--. .--'| | \ | | OO ) | | \ :` `.
| |_|( OO ) | | | |(_/ | |`-' | | |(_/ '..`''.)
| | | `-' / | | ,| |_.'(| '---.',| |_.'.-._) \
(' '-'(_.-' | | (_| | | |(_| | \ /
`-----' `--' `--' `------' `--' `-----')BANNER";
char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return find(begin, end, option) != end;
}
int main(int argc, char * argv[])
{
printf("%s\n", BANNER);
if(cmdOptionExists(argv, argv+argc, "-h"))
{
cout << "Help Menu";
}
if (cmdOptionExists(argv, argv + argc, "-a"))
{
cout << "Adobe";
}
return 0;
}
我在多个主题和多个网站上了解到,您可以使用语法 R"(<string here>)";
或 R"BANNER(<string here>)BANNER";
创建多行字符串。但是,当我开始编译这段代码时,出现以下错误:
utilis.cpp(9) : error C2001: newline in constant
utilis.cpp(9) : error C2065: 'R' : undeclared identifier
utilis.cpp(9) : error C2143: syntax error : missing ';' before 'string'
utilis.cpp(9) : error C2059: syntax error : 'string'
utilis.cpp(10) : error C2015: too many characters in constant
utilis.cpp(10) : error C2059: syntax error : ')'
utilis.cpp(11) : error C2059: syntax error : ')'
utilis.cpp(12) : error C2015: too many characters in constant
utilis.cpp(12) : error C2059: syntax error : ')'
utilis.cpp(12) : warning C4129: '_' : unrecognized character escape sequence
utilis.cpp(12) : error C2001: newline in constant
utilis.cpp(12) : error C2015: too many characters in constant
utilis.cpp(13) : error C2015: too many characters in constant
utilis.cpp(13) : error C2059: syntax error : ')'
utilis.cpp(13) : error C2059: syntax error : ')'
utilis.cpp(14) : warning C4129: ' ' : unrecognized character escape sequence
utilis.cpp(14) : error C2001: newline in constant
utilis.cpp(14) : error C2015: too many characters in constant
utilis.cpp(15) : error C2018: unknown character '0x60'
utilis.cpp(15) : error C2015: too many characters in constant
utilis.cpp(15) : error C2018: unknown character '0x60'
utilis.cpp(15) : error C2137: empty character constant
utilis.cpp(16) : error C2018: unknown character '0x60'
utilis.cpp(16) : error C2015: too many characters in constant
utilis.cpp(17) : error C2015: too many characters in constant
utilis.cpp(17) : warning C4129: ' ' : unrecognized character escape sequence
utilis.cpp(17) : error C2001: newline in constant
utilis.cpp(17) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2001: newline in constant
utilis.cpp(21) : error C2143: syntax error : missing ')' before '{'
utilis.cpp(21) : error C2143: syntax error : missing ';' before '{'
utilis.cpp(21) : error C2447: '{' : missing function header (old-style formal list?)
我做错了什么以至于无法创建多行字符串?
我也尝试过:cout << BANNER
并获得相同的输出。对我来说 to many characters in constant
错误没有意义,因为我见过比我的更大的横幅,例如 here。
问题是你把[]
const char *BANNER[] = R"BANNER(
应该是
const char *BANNER = R"BANNER(
此外,请使用 C++11 或更高版本。
使用 g++ 和 -std=c++11
.
为我工作
我正在尝试为 Windows 上的应用程序创建横幅。
// C Libraries:
#include <string>
#include <iostream>
#include <algorithm>
// Namespaces:
using namespace std;
const char *BANNER[] = R"BANNER(
.-') _ .-')
( OO) ) ( OO ).
,--. ,--. / '._ ,-.-') ,--. ,-.-') (_)---\_)
| | | | |'--...__)| |OO) | |.-') | |OO)/ _ |
| | | .-')'--. .--'| | \ | | OO ) | | \ :` `.
| |_|( OO ) | | | |(_/ | |`-' | | |(_/ '..`''.)
| | | `-' / | | ,| |_.'(| '---.',| |_.'.-._) \
(' '-'(_.-' | | (_| | | |(_| | \ /
`-----' `--' `--' `------' `--' `-----')BANNER";
char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return find(begin, end, option) != end;
}
int main(int argc, char * argv[])
{
printf("%s\n", BANNER);
if(cmdOptionExists(argv, argv+argc, "-h"))
{
cout << "Help Menu";
}
if (cmdOptionExists(argv, argv + argc, "-a"))
{
cout << "Adobe";
}
return 0;
}
我在多个主题和多个网站上了解到,您可以使用语法 R"(<string here>)";
或 R"BANNER(<string here>)BANNER";
创建多行字符串。但是,当我开始编译这段代码时,出现以下错误:
utilis.cpp(9) : error C2001: newline in constant
utilis.cpp(9) : error C2065: 'R' : undeclared identifier
utilis.cpp(9) : error C2143: syntax error : missing ';' before 'string'
utilis.cpp(9) : error C2059: syntax error : 'string'
utilis.cpp(10) : error C2015: too many characters in constant
utilis.cpp(10) : error C2059: syntax error : ')'
utilis.cpp(11) : error C2059: syntax error : ')'
utilis.cpp(12) : error C2015: too many characters in constant
utilis.cpp(12) : error C2059: syntax error : ')'
utilis.cpp(12) : warning C4129: '_' : unrecognized character escape sequence
utilis.cpp(12) : error C2001: newline in constant
utilis.cpp(12) : error C2015: too many characters in constant
utilis.cpp(13) : error C2015: too many characters in constant
utilis.cpp(13) : error C2059: syntax error : ')'
utilis.cpp(13) : error C2059: syntax error : ')'
utilis.cpp(14) : warning C4129: ' ' : unrecognized character escape sequence
utilis.cpp(14) : error C2001: newline in constant
utilis.cpp(14) : error C2015: too many characters in constant
utilis.cpp(15) : error C2018: unknown character '0x60'
utilis.cpp(15) : error C2015: too many characters in constant
utilis.cpp(15) : error C2018: unknown character '0x60'
utilis.cpp(15) : error C2137: empty character constant
utilis.cpp(16) : error C2018: unknown character '0x60'
utilis.cpp(16) : error C2015: too many characters in constant
utilis.cpp(17) : error C2015: too many characters in constant
utilis.cpp(17) : warning C4129: ' ' : unrecognized character escape sequence
utilis.cpp(17) : error C2001: newline in constant
utilis.cpp(17) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2018: unknown character '0x60'
utilis.cpp(18) : error C2015: too many characters in constant
utilis.cpp(18) : error C2001: newline in constant
utilis.cpp(21) : error C2143: syntax error : missing ')' before '{'
utilis.cpp(21) : error C2143: syntax error : missing ';' before '{'
utilis.cpp(21) : error C2447: '{' : missing function header (old-style formal list?)
我做错了什么以至于无法创建多行字符串?
我也尝试过:cout << BANNER
并获得相同的输出。对我来说 to many characters in constant
错误没有意义,因为我见过比我的更大的横幅,例如 here。
问题是你把[]
const char *BANNER[] = R"BANNER(
应该是
const char *BANNER = R"BANNER(
此外,请使用 C++11 或更高版本。
使用 g++ 和 -std=c++11
.