Help with push_back for vector <vector <long double>>

I have a little bit of code that I feel should work as is, but does not. I will paste the code below, and follow it up with the specific error that the compiler gives me. Please help!! I am a complete newbie, and at this point, very frustrated :-)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

long double get_r(vector<vector<long double> > &r, int i,int k, const int N){

  // vectors in this function are indexed by columns. To get to
  // r(i, k), I will move to the kth column r.at(k) and then down to the 
  // ith row r.at(k).at(i).

  if ((int) r.size()< k+1) // there are fewer columns than for which info is requested
    {
      // must expand vector
      r.push_back(vector <double>(k+1,0)); // adds the kth column

      for (int j =0; j<k; j++) // loop over the first k columns
	{
	  // expand each column by adding one 0 to the end
	  r.at(j).push_back(0); // this is the r(k, j) which is 0 as k > j
	}

      // Now must compute all the values in r.at(k)
      r.at(k).at(0) = 1;
      for (int j = 1; j<k; j++) // loop over the length of the column corresponding to k
	// which is also the k+1 th column
	{
	  r.at(k).at(j) = p(j, j+1, N)*r.at(k-1).at(j+1) + p(j, j-1, N) * r.at(k-1).at(j-1);
	}
      r.at(k).at(k) = p(k, k-1, N)* r.at(k-1).at(k-1);
    }

  // the above code expands the stored r values
  // now return the r(i, k) value requested
  if (i>k) {return 0;}
  else{
    return r.at(k).at(i);
  }

}


The problem is at the line that says

 
      r.push_back(vector <double>(k+1,0)); // adds the kth column 


The compiler complains by saying
1
2
 error: no matching function for call to ‘std::vector<std::vector<long double> >::push_back(std::vector<double>)’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:741:7: note: candidate is: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<long double>, _Alloc = std::allocator<std::vector<long double> >, value_type = std::vector<long double>]



EDIT: a function p(int, int, int) also shows up in the code I pasted above, but this is a function that I have already defined and tested. The error is in the code I wrote above, and not in the function p. Thanks again for the help!
Last edited on
Well, you're trying to push a vector<double> into a vector containing vector<long double>.
*facepalm*

Thank you very much.
Athar wrote:
Well, you're trying to push a vector<double> into a vector containing vector<long double>.

I just wanted to add that this is a bad thing, because it's as if you're trying to
put a basket with small apples into a storage room of baskets with big apples.
Last edited on
Yes, I need to be more careful with these things.

I have another (perhaps silly) question, but it now concerns runtime...should I post it here or start a new thread?

As a preview to the problem, code that works for N iterations gives a meaningless output for N+1 iterations.
Go ahead and post it here.
The offending code is copied below. The code compiles without errors or warnings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
int main(){
  const int Lambda = 4;
  const int N = 10;
  const int N_iter = 202;

  vector <vector <long double> >r;
  r.push_back(vector<long double> (1, 1));



 
  

  long double result;
  long double old_result;
  int k = N;


  //initialize the vector r
  for (int i = 0; i<N+1; i++)
    {
      get_r(r, i, i, N);
    }

  // double check that r(k, k) is defined
  get_r(r, k, k, N);
  old_result = alpha(k, N, Lambda)*innerloop(r, N, k);
  k++;

  get_r(r, k, k, N);
  result = old_result + alpha(k, N, Lambda)*innerloop(r, N, k);
  
  while(k<N_iter){    
    k++; // update k
    old_result = result; 
    get_r(r, k, k, N); // ensure that r(k, k)is defined
    result = result+ alpha(k, N, Lambda)*innerloop(r,N, k); //update result
    }

  cout << k << endl;


  
  cout << result<< "\t " << old_result << endl;
  return 0;
}



get_r is the function that was fixed by Athar's reply. alpha and innerloop are functions that I am not posting here because I think they work as expected. The offending value here is N_iter. For

N_iter = 202, I get the following output on rutime
1
2
202
0.0428892        0.0428892



If N_iter = 203, I get
1
2
203
-nan     0.0428892


for N_iter >= 204
1
2
204
-nan     -nan


I can't figure out what's happening.

EDIT: Will it make life simpler if I copied the entire code here?
Last edited on
Any ideas?
Last edited on
Check this out -> http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.15

Try putting isnan checks in key parts of your code to isolate the operation that causes the problem.
Okay thanks.

I discovered the problem: it was overflow I think. I was computing a factorial in one of the inner functions and I had stored the output in a double instead of a long double.

Thanks for all the help everyone.
Topic archived. No new replies allowed.