1
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cfloat>
#include <cstring>
#include <string>
using namespace std;

struct news
{
    char a[10];
    int n;
    float r;
};

int main()
{
    news k;
    cin >> k;
    cout << k.a << endl;
    cout << k.n << endl;
    cout << k.r << endl;
    return 0;
}

它给了我一个全屏的错误。有人知道如何解决这个问题吗?谢谢!

test.cc:在函数'int main()'中:test.cc:20:错误:'std :: cin >> k'中的'operator>>'不匹配/usr/include/c++/4.2.1/ istream:131: 注意:候选者是: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] /usr/include/c++/4.2.1/istream:135: 注意: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] /usr/include/c++/4.2.1/istream:142: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ istream:178: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits] /usr/include /c++/4.2.1/istream:182: 注意: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std ::char_traits] /usr/include/c++/4.2.1/istream:185: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [与 _CharT = char,_Traits = std::char_traits] /usr/include/c++/4.2.1/istream:189: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:192: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>> (unsigned int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:196: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT , _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:200: 注意:std::basic_istream<_CharT, _Traits >& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:205: 注意: std::basic_istream<_CharT, _Traits>& std: :basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:209: 注意: std:: basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1 /istream:214: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits] /usr/包括/c++/4.2.1/istream:218:注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1 /istream:222: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits] /usr /include/c++/4.2.1/istream:226: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/istream:250: 注意:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std ::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits]

4

2 回答 2

4

创建friend函数重载:

friend std::istream& operator >>(std::istream& is, news const& n)
{
    return in >> n.a >> n.n >> n.r;
}

这是您的程序的样子:

#include <iostream>

struct news
{
    char a[10];
    int n;
    float r;

    friend std::istream& operator >>(std::istream& is, news& n)
    {
        return is >> n.a >> n.n >> n.r;
    }
};

int main()
{
    news k;

    std::cin >> k;
    std::cout << k.a << std::endl;
    std::cout << k.n << std::endl;
    std::cout << k.r << std::endl;
}

现场演示

于 2013-05-12T16:00:42.203 回答
3

这不会自动发生,因为std::cin您不知道您希望如何格式化输入或如何将输入分配给news对象的成员。您必须单独阅读其每个成员:

cin >> k.a >> k.n >> k.r;

您也可以为执行类似这样的操作提供operator>>重载。news例如:

istream& operator>>(istream& in, news& cPoint)
{
  in >> k.a >> k.n >> k.r;
  return in;
}
于 2013-05-12T15:59:12.620 回答