C++: Vector of struct in header file -
i've got little question drives me crazy piece of cake guys. defined structure consists of 4 numbers , have function returns vector of structure. header file looks this:
legendre.hpp
#include <opencv2/opencv.hpp> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <sstream> #include <vector> //as suggested, doesn't #ifndef legendre_hpp_ #define legendre_hpp_ struct fourdoubles { double zeroth; double first; double second; double third; }; std::vector<fourdoubles> legendrelookuptable(int size); #endif /* legendre_hpp_ */ (actually there more functions, ignore seemingly pointless includes) have following cpp file uses header:
legendre.cpp
#include "legendre.hpp" using namespace std; using namespace cv; vector<fourdoubles> legendrelookuptable(int size) { vector<fourdoubles> res(size); int i; double x,x2,x3,x4; for(i=0;i<size;i++) { x=2.0*i/(size-1.0)-1.0; x2=x*x; x3=x2*x; x4=x3*x; res[i].zeroth=x; res[i].first=1.5*x2-0.5; res[i].second=2.5*x3-1.5*x; res[i].third=4.375*x4-3.75*x2+0.375; } return res; } just in case curious: i'm calculating legendre moments of image. however, when want compile this, compiler tells me "field 'zeroth' not resolved", same other fields.
the error occurs when comment out else in files , when main looks
#include "legendre.hpp" using namespace std; int main(int argc, char* argv[]) { } the error simple d
escription resource path location type field 'third' not resolved legendre.cpp /finder line 108 semantic error field 'second' not resolved legendre.cpp /finder line 107 semantic error field 'zeroth' not resolved legendre.cpp /finder line 105 semantic error field 'first' not resolved legendre.cpp /finder line 106 semantic error (don't mind lines, said, commented out lot of stuff)
two things quite strange me: if don't declare vector, fourdoubles directly like
vector<fourdoubles> legendrelookuptable(int size) { ... fourdoubles test; test.zeroth=5.5; ... } then works fine. also, when write whole stuff in main file directly, e.g.
struct fourdoubles { double zeroth; double first; double second; double third; }; std::vector<fourdoubles> legendrelookuptable(int size); int main() { ... } vector<fourdoubles> legendrelookuptable(int size) { ... } i'm missing basic here, don't understand why can't handle vector of struct unless it's declared in main file. enlighten appreciated.
you've forgotten include <vector> header in hpp file. that's real source of error , that's why works if declare bare structure instead of vector of structures.
Comments
Post a Comment