Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have one c++ code that used vector on that. I want call this vectors from another function from main function.

C++
int main ()
{
  vector<double> ld_12;
 vector<double> ld_24;
double d=f0(ld_12,ld_24);
.
.
.
return;
}
double f0(double *a0,double*a1){
.
.
.
}


I want know how call two vectors in header f0:
double f0(double *a0,double*a1)

Regards,
Posted
Updated 20-Feb-12 6:07am
v2

C++
int main ()
{
    vector<double> ld_12;
    vector<double> ld_24;
    double d = f0(&ld_12, &ld_24);

// ...

    return;
}

double f0(vector<double>* a0, vector<double>* a1)
{
    double dValue = 0.0;

    dValue = a0->at(0);

// ...

    return  dValue;
}
 
Share this answer
 
v2
Comments
Rezame 20-Feb-12 13:09pm    
thanks for this.
if want have this vector with complete data how must use that in f0?
for example:
vector<double>ss;
ss=a0;

how must copy ao on ss vector?
regards,
Rezame 20-Feb-12 13:55pm    
Thanks.
how to find size vector?
size_0=a0.size()
this function not works.
Richard MacCutchan 20-Feb-12 14:39pm    
this function not works.
What does that mean? Please do a proper analysis of your problem and explain what results you expect and what you receive.
Maximilien 20-Feb-12 14:02pm    
I'm not certain I understand what you mean by "if want have this vector with complete data how must use that in f0" ?

I think it depends on what you intend to do with the vectors in the f0 function, if it only to "read" from it, pass the vector by "const reference", if you want to modify it pass it by reference.
Be careful with the "=" when dealing with vectors, are you certain you want to copy (duplicate) the entire vector (and data)?
More code

C++
int main()
{
  vector<double> ld_12;
  vector<double> ld_24;

  /// ... i assume there is some code to fill the 2 vectors

  double d = f0(ld_12, ld_24);

  return 0;
}

// The & means the vectors are modifyable.
double f0 ( vector<double>& a0, vector<double>& a1 )
{
  double value = 0.0;

  // get the size of the vectors.
  size_t a0_size = a0.size();
  size_t a1_size = s1.size();

  // do something.
  return value;
}
</double></double></double></double>
 
Share this answer
 
Comments
Rezame 20-Feb-12 15:12pm    
Thanks,
After find size want do below:

li_96 = MathMin(size_0 , size_4 );
vector<double> lda_12;
vector<double> lda_16;
for(int i=0;i<li_96;i++)lda_12.push_back(ad_0[i]);
for(int i=0;i<li_96;i++)lda_16.push_back(ad_4[i]);

double x="0.0;
" double="" y="0.0;
" s="0.0;
" p1="0.0;
" p2="0.0;
" p="0.0;
" for(int="" i="0;i<li_96;i++){
" x+="lda_12[i];
" y+="lda_16[i];
" }
=""
="" =li_96;
="" s+="(lda_12[i]-x)*(lda_16[i]-y);
" p1+="(lda_12[i]-x)*(lda_12[i]-x);
" p2+="(lda_16[i]-y)*(lda_16[i]-y);
" ld_76="s/sqrt(p1*p2);
" return="" ld_76;
=""
}

after="" value="" to="" main="" code="" get="" this="" error:
1="">Source\ea.cpp(276): error C2664: 'f0_55' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> *'


Regards,
2 options :

1- If possible, refactor the code to have f0 receive vectors instead of arrays.
2- call f0 like :

C++
f0( &ld_12[0], &ld_24[0] );


This works because the standard tells us that the vector is allocated in continuous memory (like an arrary).

Also, don't forget to pass the size of the 2 arrays somewhere to your f0 function.
 
Share this answer
 
Comments
Rezame 20-Feb-12 13:46pm    
I can receive vector too.
must do same solution 1?
Maximilien 20-Feb-12 13:52pm    
Always use better programming practices: use vector instead of C arrays.
Rezame 20-Feb-12 13:56pm    
Thanks. how to find size vector?
size_0=a0.size()
this function not works.
Thanks,
After find size want do below:

C++
li_96 = MathMin(size_0 , size_4 ); vector lda_12; vector lda_16;
for(int i=0;i<li_96;i++)lda_12.push_back(ad_0[i]);
   for(int i=0;i<li_96;i++)lda_16.push_back(ad_4[i]);
  

   double x=0.0;
   double y=0.0;
   double s=0.0;
   double p1=0.0;
   double p2=0.0;
   double p=0.0;
   for(int i=0;i<li_96;i++){
   x+=lda_12[i];
   y+=lda_16[i];
   }


   x/=li_96;

   y/=li_96;
   for(int i=0;i<li_96;i++){
   s+=(lda_12[i]-x)*(lda_16[i]-y);
   pli_96 = MathMin(size_0 , size_4 ); vector lda_12; vector lda_16;
for(int i=0;i<li_96;i++)lda_12.push_back(ad_0[i]);
   for(int i=0;i<li_96;i++)lda_16.push_back(ad_4[i]);
  

   double x=0.0;
   double y=0.0;
   double s=0.0;
   double p1=0.0;
   double p2=0.0;
   double p=0.0;
   for(int i=0;i<li_96;i++){
   x+=lda_12[i];
   y+=lda_16[i];
   }


   x/=li_96;

   y/=li_96;
   for(int i=0;i<li_96;i++){
   s+=(lda_12[i]-x)*(lda_16[i]-y);1+=(lda_12[i]-x)*(lda_12[i]-x);
   p2+=(lda_16[i]-y)*(lda_16[i]-y);
   }

   ld_76=s/sqrt(p1*p2);

   return ld_76;


After return ld_76 i get this eror:
ea.cpp(276): error C2664: 'f0_55' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> *'

Regards,
 
Share this answer
 
Comments
Maximilien 20-Feb-12 15:24pm    
There is no f0_55 in the code you just posted; I need more to help you.

Please post relevant code (don't forget to copy and paste code with the {} ).
Stefan_Lang 21-Feb-12 5:31am    
Please do not submit your responses as solutions! 'Solution' means a response to your question that may solve your problem.

If your intention is to add information about your problem, use the green Improve Question link below your original question.

If your intention is to respond to a particular solution that someone suggested, click the blue Have a Question or Comment below that solution.

If your intention is to reply to a comment, click the Reply link to the right of that comment.

It's very hard to follow a discussion, if the relevant information is spread over several 'solutions' that are in fact part of the question. Also, if you do not use the proper links to respond to other peoples contributions, they will not be notified of the fact that you responded, and therefore may not react to your new information or questions.
Rezame 21-Feb-12 5:33am    
thanks
Stefan_Lang 21-Feb-12 6:20am    
I suspect you hve two definitions for the function fo_55() in your code: one from solution 1, and another based on solution 3. Get rid of the first, and place the other on top of the function that calls it.
Here is complete code:


C++
int f0_4(BarData *bars, int barcount,int ai_0, int ai_4, int ai_8) {  //li_24, BoxWidth, SearchDepth
   vector<double>lda_112;
   vector<double>lda_24;
   vector<double>lda_241;
    vector<double>lda_242;
	 vector<double>lda_243;
	  vector<double>lda_28;
	   vector<double>lda_281;
	   vector<double>lda_282;
	   vector<double>lda_283;
	   
int li_ret_60=0;
   int count_64;
   double ld_40 = -999999;
  double gda_152=0.0;
   double gia_148=0.0;
  
   
   for (int index_48 = 0; index_48 < ai_4; index_48++) {
	   lda_24.push_back(bars[bar_index(ai_0 + index_48, barcount)].close);
    // lda_24[index_48] = bars[bar_index(ai_0 + index_48, barcount)].close;
     lda_241[index_48] = bars[bar_index(ai_0 + index_48, barcount)].open;
lda_242[index_48] = bars[bar_index(ai_0 + index_48, barcount)].high; 
 lda_243[index_48] = bars[bar_index(ai_0 + index_48, barcount)].low;  
}
   for (int index_52 = ai_4; index_52 < ai_8; index_52++) {
      
         for (int index_48 = 0; index_48 < ai_4; index_48++) {
           lda_28[index_48] = bars[bar_index(index_48 + index_52, barcount)].close;
           lda_281[index_48] = bars[bar_index(index_48 + index_52, barcount)].open;
           lda_282[index_48] = bars[bar_index(index_48 + index_52, barcount)].high;
           lda_283[index_48] = bars[bar_index(index_48 + index_52, barcount)].low;
           
           
         }
         
		 lda_112.push_back(f0_55(lda_24, lda_28));
         if (lda_112[index_52] > ld_40) {
            ld_40 = lda_112[index_52];
            li_ret_60 = index_52;
            
       //  }
      }
   }
   
 
   gda_152= ld_40;
   gia_148= li_ret_60;
 
   
   return li_ret_60;
}



   double f0_55(vector<double>& ad_0, vector<double>& ad_4) {
   double ld_44;
   double ld_52;
   int li_92;
   int li_96;
   double ld_76 = 0.0;

 
   int size_0 = ad_0.size();
  int size_4 = ad_4.size();
   li_96 = MathMin(size_0 , size_4 );
   vector<double> lda_12;
   vector<double> lda_16;
   
   
 for(int i=0;i<li_96;i++)lda_12.push_back(ad_0[i]);>
   for(int i=0;i<li_96;i++)lda_16.push_back(ad_4[i]);>
   
   
   double x=0.0;
   double y=0.0;
   double s=0.0;
   double p1=0.0;
   double p2=0.0;
   double p=0.0;
   for(int i=0;i<li_96;i++){>
   x+=lda_12[i];
   y+=lda_16[i];
   }
   
   
   x/=li_96;
   
   y/=li_96;
   for(int i=0;i<li_96;i++){>
   s+=(lda_12[i]-x)*(lda_16[i]-y);
   p1+=(lda_12[i]-x)*(lda_12[i]-x);
   p2+=(lda_16[i]-y)*(lda_16[i]-y);
   }
   
   ld_76=s/sqrt(p1*p2);
  
   return ld_76;
   
}
</double></double></double></double></double></double></double></double></double></double></double></double></double>
 
Share this answer
 
v2
Comments
Legor 21-Feb-12 11:30am    
Dont post you comments as new Questions. You can use the "Improve Question" Button to update your question.

And to you question it's really not clear what you want to do or what isn't working. It also seems to me you can't answer these questions since id guess you got the code from somewhere else and now trying to adapt it somehow by force without putting too much thought into it. just my expression though..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900