C++字符串变量声明
我在声明字符串变量时遇到一些问题。代码和错误在这里: http://pastebin.com/TEQCxpZd 关于我做错了什么的任何想法?另外,请保持平台独立。谢谢!
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string input; //Declare variable holding a string
input = scanf; //Get input and assign it to variable
printf(input); //Print text
return 0;
}
Getting this from GCC:
main.cpp: In function ‘int main()’:
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’
main.cpp:53:10: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’
main.cpp:54:14: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’
I'm having some trouble declaring a string variable. Code and the errors are here: http://pastebin.com/TEQCxpZd Any thoughts on what I'm doing wrong? Also, please keep it platform independent. Thanks!
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string input; //Declare variable holding a string
input = scanf; //Get input and assign it to variable
printf(input); //Print text
return 0;
}
Getting this from GCC:
main.cpp: In function ‘int main()’:
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’
main.cpp:53:10: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’
main.cpp:54:14: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在混合 c++ 和 c I/O。在 C++ 中是这样的:
You are mixing c++ and c I/O. In C++ this is,
我理解这个问题是:如何在 C++ 中进行字符串声明?
下面是一个简短的程序来演示:
因此,在程序的开头包含 cstdlib。实际上,这意味着输入 string 而不是 std::string,输入 cout 而不是 std::cout 等等。字符串变量本身(在示例中,字符串变量是 your_name)用 string 声明。
假设您已使用文件名“str_example.cpp”保存了程序
在命令行(Linux 中)编译程序:
这将创建一个名为 str_example(无文件扩展名)的可执行目标文件。
最后,假设您与程序位于同一目录中,运行它:
g++ 的手册页很丰富,但默认情况下不包含在内。要使用 aptitude 包管理器安装 g++ 文档:
请注意,“7”指的是版本 7;撰写本文时的当前版本。希望有帮助。
I understand the question to be: How do you make a string declaration in C++?
Here's a short program to demonstrate:
So, include cstdlib at the start of your program. In practical terms, this means typing string instead of std::string, cout instead of std::cout and so on. The string variable itself (in the example, the string variable is your_name) is declared with string.
Let's say you've saved the program with the filename, 'str_example.cpp'
To compile the program at the command line (in Linux):
This creates an executable object file called str_example (no file extension).
And finally, assuming you're in the same directory as the program, to run it:
The man page for g++ is extensive but not included by default. To install g++ documentation using the aptitude package manager:
Note that the '7' refers to version 7; the current version at the time of writing. Hope that helps.
您试图将
scanf
的函数指针分配给一个字符串变量。你不能这样做,这就是你收到第一个错误的原因。正确的语法是。但这是一种非常 C 风格的做事方式。在 C++ 中读取输入的惯用方法是使用
std::cin >>>按照内森的建议输入
。printf
采用const char*
作为其第一个参数,而不是std::string
。您可以使用.c_str()
转换为 C 样式字符串。但从不将用户输入作为第一个参数传递给printf
;用户可以通过在字符串中放入%
来做一些令人讨厌的事情。如果您坚持使用 C 样式输出,则正确的语法是:但 C++ 样式的替代方案是
std::cout <<输入;
。You're trying to assign the function pointer to
scanf
to a string variable. You can't do that, which is why you're getting the first error. The proper syntax would be.But that's a very C-style way of doing things. The idiomatic way to read input in C++ is with
std::cin >> input
as Nathan suggested.printf
takes aconst char*
as its first argument, not astd::string
. You can use.c_str()
to convert to a C-style string. But never pass user input as the first argument toprintf
; the user can do nasty stuff by putting%
's in the string. If you insist on C-style output, the correct syntax is:But the C++-style alternative is
std::cout << input;
.