Tôi đang gặp rắc rối với các mã sau và dường như không thể tìm ra những gì là saiSử dụng các vector trong C++
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
double distance(int a, int b)
{
return fabs(a-b);
}
int main()
{
vector<int> age;
age.push_back(10);
age.push_back(15);
cout<<distance(age[0],age[1]);
return 0;
}
Lỗi nằm ở gọi về khoảng cách hoạt động.
/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
test.cpp:18:30: instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:170:53: error: ‘int’ is not a class, struct, or union type
gì bạn muốn làm gì? –
Tôi muốn lưu trữ dữ liệu trong một mảng vectơ (đối với kích thước động), sau đó tính khoảng cách giữa các điểm dữ liệu. –