1)VECTOR.h(984,2)无法创建预编译的头:头中的代码 forecast.resize(size); //我指定了向量的大小 他后来使用push_back将值添加到向量中,其中 使用调整大小而不是保留的情况是致命的。 Hi all,I am using "vectors" and also Borland C++ compiler for fist time. Iwrote the following code./************************************************** *********/#include<stdio.h>#include<iostream.h>#include <vector>#include <algorithm>#include <numeric>class Moving_Average{private: // Moving_Average class will contain the vector forecastvector<double> forecast;public:void Moving_Average(int size){forecast.reserve(size); //I am specifying the size of vectorhere}void calculate(double *da, int time){/* This function will calculate the moving averages (one method offorecasting)and then store the values in the vector*/double sum=0;for(int i=1;i<=time;i++) {sum+=*(da+i);}cout<<"sum of the first tge "<<sum<<endl;forecast.pushback(sum/time);for(int i=time+1;i<=size;i++) {forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time));}for(int i=1;i<=forecast.size();i++) {cout<<"moving average for period "<<i<<" -->"<<forecast[i]<<endl;}}};void main(){Moving_Average MA;cout<<"Enter the no.of periods of historic data"<<endl;cin>>periods;MA(periods);vector<double>data(periods); // This vector holds the datafor(int i=1;i<=periods;i++) {cout<<"Enter data for period --> "<<i<<" ";cin>>data[i];}MA.calculate(data.begin(),2);}/******************************************* /I am getting following erros.1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header2)tria.cpp(9,14):Type name expected3)tria.cpp(9,14):Declaration missing;and so on..Pls mail me what is wrong with my code.Thanks in advance..Chandrashekar 解决方案 Chandrashekar wrote: Hi all, I am using "vectors" and also Borland C++ compiler for fist time. I wrote the following code. /************************************************** *********/ #include<stdio.h> #include<iostream.h><iostream.h> is a non-standard header. Use <iostream> instead, and<stdio.h> is deprecated and should be replaced by <cstdio>. But itdoesn''t seem that you use anything from that header anyway. #include <vector> #include <algorithm> #include <numeric> class Moving_Average{ private: // Moving_Average class will contain the vector forecast vector<double> forecast;vector is in namespace std. So write:std::vector<double> forecast; public: void Moving_Average(int size) { forecast.reserve(size); //I am specifying the size of vector here } void calculate(double *da, int time){ /* This function will calculate the moving averages (one method of forecasting) and then store the values in the vector */ double sum=0; for(int i=1;i<=time;i++) { sum+=*(da+i);That''s not an error, but it would look cleaner if you wrote:sum+=da[i];I wonder: does da really point to time+1 values, and why aren''t youusing the first value? Remember, indexes start at 0. } cout<<"sum of the first tge "<<sum<<endl;cout and endl are also in namespace std. forecast.pushback(sum/time); for(int i=time+1;i<=size;i++) {Where is size defined?forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time)); } for(int i=1;i<=forecast.size();i++) { cout<<"moving average for period "<<i<<" --> "<<forecast[i]<<endl; } } }; void main()main() must return int in C++. Some compilers let you get away with it,but it''s still an error. { Moving_Average MA;Here you create a Moving_Average object using the default constructor.Unfortunately, that class doesn''t have a default constructor, so youcan''t do that. cout<<"Enter the no.of periods of historic data"<<endl; cin>>periods; MA(periods);Is that supposed to construct the MA object? You can''t do that. Thiswill instead try to call operator() of the Moving_Average class. Sincethere is no operator() defined, this can''t work. You cannot constructan object twice. Either defer the construction to this place, i.e.remove the first line of main() and write here:Moving_Average MA(periods);or create an own member function to specify the size. Anyway,reserve()ing the space in a vector is only an optimization. It''s notmandatory. If you don''t do that, push_back() will automaticallyallocate the needed space. vector<double>data(periods); // This vector holds the data for(int i=1;i<=periods;i++) { cout<<"Enter data for period --> "<<i<<" "; cin>>data[i];Here, you have the index problem again (and at various other places).You don''t put anything in the first element of the vector and you writeone element past its end. } MA.calculate(data.begin(),2);Why 2? Shouldn''t it be periods? Also, you can''t specify data.begin()here. This function returns an iterator to the first element of thevector. While a vector iterator can be a pointer, it''s not required to,and some compilers (like gcc) have an own iterator class. Use &data[0]instead. } /******************************************* / I am getting following erros. 1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header 2)tria.cpp(9,14):Type name expected 3)tria.cpp(9,14):Declaration missing; and so on..You should have listed all of them and marked the lines your compiler isreferring to. It makes it a lot easier to find the errors. Pls mail me what is wrong with my code.No. This is not a write-only newsgroup. Post here - read here."Chandrashekar" <ch******@bhelhyd.co.in> wrote in messagenews:94*************************@posting.google.co m... Hi all, I am using "vectors" and also Borland C++ compiler for fist time. I wrote the following code. /************************************************** *********/ #include<stdio.h> #include<iostream.h> #include <vector> #include <algorithm> #include <numeric> class Moving_Average{ private: // Moving_Average class will contain the vector forecast vector<double> forecast; public: void Moving_Average(int size) { forecast.reserve(size); //I am specifying the size of vector hereNo you aren''t, you are reserving memory for the vector, its size is stillzero.If you want to resize a vector use resize.forecast.resize(size); //I am specifying the size of vectorjohnJohn Harrison wrote: "Chandrashekar" <ch******@bhelhyd.co.in> wrote in message news:94*************************@posting.google.co m... Hi all, I am using "vectors" and also Borland C++ compiler for fist time. I wrote the following code. /************************************************** *********/ #include<stdio.h> #include<iostream.h> #include <vector> #include <algorithm> #include <numeric> class Moving_Average{ private: // Moving_Average class will contain the vector forecast vector<double> forecast; public: void Moving_Average(int size) { forecast.reserve(size); //I am specifying the size of vector here No you aren''t, you are reserving memory for the vector, its size is still zero.That''s actually what the OP meant, I''d say. If you want to resize a vector use resize. forecast.resize(size); //I am specifying the size of vectorHe''s later using push_back to add the values to the vector, in whichcase using resize instead of reserve is fatal. 这篇关于帮助矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!