C ++语法错误 - 无法识别问题

时间:2013-10-05 11:52:20

标签: c++ vector syntax-error

我是C ++的新手并且在理解它的行为方面遇到了一些麻烦,因为它与我研究过的其他语言完全不同。

有人可以指出我正确的方向来修复此代码并解释错误日志吗?

我使用http://www.compileonline.com/compile_cpp_online.php编译并运行此代码。

数组(向量)变量声明为大小为5,然后while循环内的for循环将填充它并更改值。在while的第一个循环中,for循环填充它,如果输入的第一个值是-1,则while循环应该停止。否则,请继续允许用户更改值。

#include <iostream>
#include<vector>

using namespace std;

int main()
{
    vector<int> items(5); 

    do
    {
        cout << "Type customer details: " << endl;

        for(int i = 0; i < 5; i++)
        {
            cout << "info " << 0 << ": ";

            items[i] >> cin;
        }

        int newBalance = items[1] + items[2] - items[3];

        if(newBalance > items[4])
        {
            cout << items[0] << " - " << items[4] << " - " << newBalance << " - Credit limit exceeded." << endl;
        }

    }while(items[0] != -1);
}

错误日志:

Compiling the source code....
$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function 'int main()':
main.cpp:18:13: error: no match for 'operator>>' (operand types are 'int' and     'std::istream {aka std::basic_istream<char>}')
items[i] >> cin;
^
main.cpp:18:13: note: candidates are:
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:53:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/locale_classes.h:40,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/ios_base.h:41,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/ios:42,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/ostream:38,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:39,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note:     template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>&     std::operator>>(std::basic_istream<_CharT, _Traits>&,     std::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note: template     argument deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note: template<class     _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>&     std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note: template<class     _CharT, class _Traits> std::basic_istream<_CharT, _Traits>&     std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note: template<class     _Traits>     std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char,     _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note: template<class _Traits>     std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char,     _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note: template<class _Traits>     std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char,     _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note: template<class _Traits>     std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char,     _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note: template argument     deduction/substitution failed:
main.cpp:18:16: note: mismatched types 'std::basic_istream<char, _Traits>' and 'int'
items[i] >> cin;
^

2 个答案:

答案 0 :(得分:4)

你需要

#include <vector>

请参阅vector在线页面

items[i] >> cin;  // this line is not valid

应该是

std::cin >> item[i];

或者从控制台调用std::getline,转换为int,然后将值赋给vector中的元素。

答案 1 :(得分:2)

替换

items[i] >> cin;  

cin >> items[i];