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&) [ with _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/ include/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 に答える