我在声明字符串变量时遇到了一些问题。代码和错误在这里: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*, ...)’
最佳答案
您正在混合使用 C++ 和 C I/O。在 C++ 中,这是,
#include <string>
#include <iostream>
int main(void)
{
std::string input;
std::cin >> input;
std::cout << input;
return 0;
}
关于C++ 字符串变量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4743564/