C++字符串变量声明

9
我遇到了一些声明字符串变量的问题。代码和错误在这里: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*, ...)’

可能来自Pascal,你可以在不使用()的情况下调用函数。 - dan04
很奇怪,C++不是Pascal。 - GManNickG
3个回答

11
你混淆了C++和C的输入输出。在C++中,应该这样写:
#include <string>
#include <iostream>

int main(void)
{
   std::string input;
   std::cin >> input;
   std::cout << input;
   return 0;
 }

1
@Mike:你是否已经包含了iostream头文件?#include <iostream> 另外,编译C++程序时应该使用g++而不是gcc。 - Cody Gray
我已经包含了iostream。问题在于终端中我必须键入g++ main.cpp -o hello_world而不是gcc。谢谢Cody! - Mike
1
@Mike:不用客气。请别误解,但看起来你对 C 和 C++ 有些混淆。它们实际上是不同的语言,尽管其中一个在很大程度上源自另一个,并且它们共享许多相同的语法。如果你想学习这门语言,你需要选择一种。正如两个答案所示,C++(与C相比)有非常不同的习惯用法,即使是读取输入这样简单的事情也是如此。除了标题外,你的所有内容都给人一种可能是在使用 C 的印象,这解释了为什么你编写了你的代码并使用 gcc。 - Cody Gray
我下载了一个使用stdio创建hello world的C++ IDE,所以我认为它应该可以编译,但发生了几件事情:1. 我无法在IDE中构建项目,2. 我不太了解GCC,不知道它不能编译C和C++,我以为G++可以让GCC编译C++,3. 我所有关于C++的知识都是在Windows上,而且非常有限,我认为如果iostream无法工作,那么这是一个Windows头文件的问题。现在我没问题了,可以继续使用我得到的这本应该是平台无关的C++书,但由于我使用的是GCC而不是G++,所以它无法工作。再次感谢您的帮助! - Mike

3

我理解你的问题是:如何在C++中声明字符串? 这里有一个简短的程序来演示:

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    string your_name;
    cout << "Enter your name: ";
    cin >> your_name;
    cout << "Hi, " << your_name << "!\n";
    return 0;
}

因此,在程序开始处包含 cstdlib。在实际操作中,这意味着输入 string 而不是 std::string,cout 而不是 std::cout 等等。例如,字符串变量(在示例中,字符串变量为 your_name)使用 string 声明。

假设您已将程序保存为文件名“str_example.cpp” 要在命令行(Linux 中)编译该程序:

g++ -o str_example str_example.cpp

这将创建一个可执行的对象文件,名为str_example(没有文件扩展名)。最后,假设您在程序所在的同一目录中,要运行它:

./str_example

g++的man页面非常详细,但默认情况下不包含在内。使用aptitude软件包管理器安装g++文档:

sudo apt-get install gcc-7-doc

请注意,此处的“7”指代版本7;即本文撰写时的当前版本。希望这能帮到您。

2

无法将'std::string'转换为'const char *',以便将参数'1'传递给'int printf(const char *, ...)'。

input = scanf; //Get input and assign it to variable

你试图将函数指针 function pointer 分配给字符串变量中的 scanf 。你无法这样做,这就是为什么出现了第一个错误。正确的语法应该是:
char buffer[BIG_ENOUGH_SIZE];
scanf("%*s", sizeof(buffer) - 1, buffer);
input = buffer;

不过那是一种非常C语言式的做法。在C++中,惯用的读取输入方式是使用Nathan建议的std::cin >> input

无法将 'std::string' 转换为 'const char*',因此无法将其作为参数传递给 'int printf(const char*, ...)'。

printf(input); //Print text

printf函数的第一个参数应该是const char*类型的,而不是std::string类型的。你可以使用.c_str()将其转换为C风格字符串。但绝不要将用户输入作为printf的第一个参数;因为用户可能会在字符串中插入%字符来执行恶意操作。如果你坚持使用C风格输出,正确的语法是:

printf("%s", input.c_str());

但是C++风格的替代方法是std::cout << input;


我甚至连Hello World都无法正确编译。我觉得编译器出了问题,不管代码对不对,我知道它没有任何问题。 #include <iostream> using namespace std;int main() { cout << "Hello World!" << endl; return 0; } - Mike
这里更有趣的是,在使用MSVC编译时,从std::string到const char的隐式转换成功了,但在使用GCC编译时却不行。我想这是因为MSVC实现的std::string包含了一个用于const char的转换运算符,而GCC实现则没有? - Thomas Sampson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接