c++ 程序无法正常运行

c++ 程序无法正常运行
/* write a prog to take the name of the user and output it with a thank you stamement*/

#include<iostream>
using namespace std;
int main()
{
 char ch[20];
 cout<<"enter your name pls"<<endl;
  cin >> ch>>endl;
  cout<<"thank you::"<<ch<<endl;
 return 0;
}

输出为:

ff@ff-desktop:~/CCCP/ch1$ g++ 2.cpp -o 2
2.cpp: In function ‘int main()’:
2.cpp:9: error: no match for ‘operator>>’ in ‘std::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& std::cin)), ((char&)(& ch))) >> std::endl’
/usr/include/c++/4.4/istream:119: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:123: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:130: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:166: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:173: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:180: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:193: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:202: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:238: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:741: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/istream.tcc:853: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
ff@ff-desktop:~/CCCP/ch1$

谁能告诉我出了什么问题?

ACTIONPARSNIP(ANDREW-WOODHEAD666)建议这样做:

尝试改变: #include <iostream>

到: #include "iostream"

或者尝试: #include <iostream.h>

或者: #include "iostream.h"

经过这些改变后,我得到了以下结果。

下面是上述相同程序的输出,但"iostream.h"使用 代替<iostream.h>

2.cpp:2:21: error: iostream.h: No such file or directory
2.cpp: In function ‘int main()’:
2.cpp:8: error: ‘cout’ was not declared in this scope
2.cpp:8: error: ‘endl’ was not declared in this scope
2.cpp:9: error: ‘cin’ was not declared in this scope

有人可以帮忙并告诉我哪里出了问题吗?

答案1

#include <iostream>是正确的。(实际上,您使用了#include<iostream>,没有空格,但这也是正确的。)包括"iostream.h"<iostream.h>都是错误的,尽管一些编译器和标准库会接受它们。

这个问题是因为标准库没有提供operator>>可以std::istream在左边取一个而std::ostream&在右边取一个的重载。更直白地说,这就是你的错误发生的地方:

cin >> ch>>endl;
  • 请注意,这相当于(cin >> ch) >> endl,并且>>错误发生在括号外面,尽管将括号改为尝试将chendl作为参数传递给>>也是错误的。
  • endl是并且ostream&cinistream不能>>istream左边的和ostream&右边的进行运算,因为 STL 没有定义可以为这样的表达式选择的任何重载。

您可以使用endlwith cout,但不能像这样使用 with cin

如何解决这个问题取决于你故意的去做。

  • 如果您的目标是在输入后打印换行符,请cout << endl在此之后添加单独的语句(),或者将已有的语句添加endl到后续语句中。cout

  • 如果你的目标是在遇到换行符时停止输入,那么已经发生与使用<<with cin,因此只需删除endl部分:

    cin >> ch;
    
  • 如果你的目标是保持当有任何空格时接受输入除了遇到换行符(即您要确保将一行输入准确地读入数组charstd::string),则必须使用更复杂的表达式,甚至根本不使用operator>>with cin

    因为不清楚你想要什么,而且实现该解决方案不是特定于平台的(而 STL 实现中可用的重载至少是理论上由于平台特定),关于如何做到这一点的细节超出了本答案的范围。但如果这就是你想要做的,我建议询问堆栈溢出

答案2

您应该使用:(#include <iostream>不要忘记空格)和using namespace std;

按照以下语法进行编译:g++ -o 2 2.cpp

如果这不起作用,则:find /usr/include -name iostream -type f -print并将其添加到您的包含路径中

相关内容