string str1 = "hello";
const char* string1 = str1;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误..
在初始化时无法将'std :: string {aka std :: basic_string}'转换为'const char*'
我如何将字符串转换为const char*
谢谢你的帮助
我运行这段代码时遇到错误
string line;
getline (myfile,line);
char file_input[15];
strncpy(file_input, line, 6);
cout << line << endl;
Run Code Online (Sandbox Code Playgroud)
错误 - 无法将'std :: string'转换为'const char*'以将参数'2'转换为'char*strncpy(char*,const char*,size_t)'如何摆脱这个?
我已经更改了我的代码,现在编译时出现这些错误:
`check.cpp: In function ‘int main()’:`
Run Code Online (Sandbox Code Playgroud)
check.cpp:14:55: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
/usr/include/getopt.h:152:12: error: initializing argument 2 of ‘int getopt(int, char* const*, const char*)’ [-fpermissive]
int main() {
string text="-f input.gmn -output.jpg";
int argc=text.length();
cout<<"argc: "<<argc<<endl;
char const * argv = text.c_str();
cout<<"argv: "<<argv<<endl;
int c = getopt (argc, &argv, "f:s:o:pw:h:z:t:d:a:b:?");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我用C++编程.
这个问题基本上是我无法在任何地方找到答案.所以这是问题所在:
我想创建一个C风格的字符串,但我想将一个整数变量i放入字符串中.很自然地,我使用了一个流:
stringstream foo;
foo
<< "blah blah blah blah... i = "
<< i
<< " blah blah... ";
Run Code Online (Sandbox Code Playgroud)
但是,我需要以某种方式获得一个C风格的字符串传递给一个函数(原来foo.str()返回一个std :: string).所以这在技术上是一个三部分问题 -
1)如何将std :: string转换为C风格的字符串?
2)有没有办法从字符串流中获取C风格的字符串?
3)有没有办法直接使用C风格的字符串(不使用字符串流)来构造一个带有整数变量的字符串?
我有一个std::string并希望将其作为文件名传递给fstream
std::string fname = "/home/mahmood/filter" + boost::lexical_cast<std::string>(b) + ".txt";
std::fstream fout (fname, std::fstream::app | std::fstream::out);
Run Code Online (Sandbox Code Playgroud)
但我得到了第二行的错误
error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(std::string&,
Run Code Online (Sandbox Code Playgroud)
似乎它无法转换string为char *.此外,铸造不起作用
我试着稍微研究一下,我无法弄清楚问题
这是代码:
#include <iostream>
#include <stdlib.h>
#include <string>
void choose();
void newuser();
void admuse();
using namespace std;
string x;
string z;
string w;
void CreNeAcc(){
cout << "Enter User Name for your new account\n";
getline(cin, x);
cout << "Enter Password for your new account\n";
getline(cin, z);
cout << "Would you like the account to be admin?\n";
cout << "Yes = Y, No = N\n";
getline(cin, w);
choose();
}
void choose(){
if(w == "Y"){
newuser();
admuse();
}else if(w == "N"){
newuser(); …Run Code Online (Sandbox Code Playgroud) 我正在使用C++(使用CERN的ROOT框架),我对字符串有一点问题.我正在尝试使用代码中较早的用户定义的字符串来标记直方图轴.以下是代码的相关部分:
string xlabel;
...
cout << "Enter x-axis label:" << endl;
getline(cin >> ws, xlabel);
...
hist->GetXaxis()->SetTitle(xlabel);
Run Code Online (Sandbox Code Playgroud)
最后一行只是ROOT使用的语法(通常这里的xlabel将在引号中,你可以键入你想要的标签,但我试图输入前面在代码中定义的字符串.)
无论如何,当我编译它时,我收到以下错误:
error: no viable conversion from 'string'
(aka 'basic_string<char>') to 'const char *'
hist->GetXaxis()->SetTitle(xlabel);
^~~~~~
Run Code Online (Sandbox Code Playgroud)
我曾尝试将xlabel重新定义为const char*,但它也不喜欢它.有没有人对如何定义这个字符串有任何建议?
提前致谢!
如标题所示,该问题是我在密码功能的特定部分执行程序时出错。实际上,这是一个基本的密码功能,在Turbo C ++中正常工作,但是在Visual C ++中,此错误到来
void user::password()
{
char any_key, ch;
string pass;
system("CLS");
cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER
PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
start:
getline(cin,pass);
if (strcmp(pass, "sha") == 0) //this is where the error is!*
{
cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
cin >> any_key;
}
else
{
cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
goto start;
}
system("CLS");
}
Run Code Online (Sandbox Code Playgroud) 我尝试了两种方法将我的char*类型转换ss
为双精度类型。这是我的代码(在VC++6.0 windows64位编译)
int main()
{
char *ss = "-1964734.544";
cout<<ss<<endl;
cout<<*reinterpret_cast<double*>(ss)<<endl;
cout<<*(double *)ss<<endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
-1964734.544
3.06123e-057
3.06123e-057
Run Code Online (Sandbox Code Playgroud)
我不清楚出了什么问题以及如何将 a 转换char*为 double 。
我以前从未见过这个函数的调用char()。这是在哪里描述的,它是什么意思?此用法是此 cppreference.com 社区 wiki 页面上示例的一部分:https://en.cppreference.com/w/cpp/string/basic_string/resize:
short_string.resize( desired_length + 3 );
std::cout << "6. After: \"";
for (char c : short_string) {
std::cout << (c == char() ? '@' : c); // <=== HERE ===
}
Run Code Online (Sandbox Code Playgroud)
描述中的措辞对我来说也没有任何意义,我不明白它在说什么:
将附加字符初始化为
CharT().
在上下文中突出显示:
std::string::resize()方法的原因是尝试学习如何预先分配 astd::string以便在 C 函数调用中用作缓冲区char*。这可以通过首先std::string调用my_string.resize()函数来预分配 来实现。然后,您可以安全地&my_string[0]作为标准char*写入索引my_string.size() - 1。也可以看看: