我正在尝试为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或更高版本。
适用于带有
-std=c++11
的g++。关于c++ - C++多行字符串无法按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43005416/