我有这段代码,它读取用户代码并将其存储在数组中,然后在每个数组上找到元素的总和并比较它们,代码如下:
#include <iostream>
#include <vector>
#include <numeric>
typedef std::vector<int> int_vec_t;
//Call by reference to set variables in function
void readData(int_vec_t& v1, int_vec_t& v2)
{
v1 = int_vec_t{1,1,8}; //This only works for C++11
v2 = int_vec_t{2,2,2};
}
void readUserData(int_vec_t& v)
{
for(;;)
{
int val;
std::cin>>val;
if(val == 0) break;
v.push_back(val);
}
}
int main()
{
using namespace std;
int_vec_t A;
int_vec_t B;
readData(A,B);
//Or
readUserData(A);
readUserData(B);
int sumA = accumulate(A.begin(), A.end(), 0); //Then use iterators
int sumB = accumulate(B.begin(), B.end(), 0);
cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");
return 0;
}
但是上面的代码会产生以下错误:
test.cpp: In function ‘void readData(int_vec_t&, int_vec_t&)’:
我g++ test.cpp -o test
用来编译代码。我在这里想念什么?