C++ question

Status
Not open for further replies.

tacosareveryyummy

Supreme [H]ardness
Joined
Jul 25, 2005
Messages
5,300
I have some rough code that I am trying to compile. It isn't immediately obvious to me what the issue is. Hopefully it's simple and someone here can help.

Code:
#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<vector>
#include<cstdlib>
using namespace std;

int poisson(double);
float expon(double);
float one_body_weight();
float rand_val(int);

int main()
{
  int N_e=10;
  int lambda=10;
  int number=100;
  int poi_rv=0;
  int step=0;
  int initial_orbital=0;
  int final_orbital=0;
  srand(1);

  ifstream infile;
  ofstream outfile;

  //  vector< vector<int> > C(10, vector<int>(N_e)); // Initial state
  vector< vector<int> > C_prime(10, vector<int>(N_e)); //Final state
  vector<int> state_vector(10);
  //  vector<int> n_i(10,0); // Initial state to be replicated

  outfile.open("p_values.dat"); //Data file for number of steps

  for(int l=0; l<N_e; l++)
    {
      poi_rv = poisson(1.0/lambda);
      outfile << poi_rv;
    }

  outfile.close();
  infile.open("p_values.dat"); 

 for(int i=0; i<=N_e; i++)
    {      
      infile >> step;

      state_vector[0] = 1;
      state_vector[1] = 0;
      state_vector[2] = 0;
      state_vector[3] = 0;
      state_vector[4] = 0;
      state_vector[5] = 0;
      state_vector[6] = 0;
      state_vector[7] = 0;
      state_vector[8] = 0;
      state_vector[9] = 0;

      for(int p=0; p<=step; p++)
	{
	  initial_orbital = rand()%10;
	  final_orbital = rand()%10;
	  
	  if(state_vector[final_orbital] == 0 && state_vector[initial_orbital] != 0 ) // Orbital unoccupied then move pair to new location
	    {
	      state_vector[final_orbital] = state_vector[initial_orbital];
	      state_vector[initial_orbital] = 0;
	    }
	  else if(state_vector[final_orbital] != 0 && state_vector[initial_orbital] != 0) //If orbital is occupied and equal to current orbital then switch
	    {
	      int storage=0;
	      storage = state_vector[final_orbital];
	      state_vector[final_orbital] = state_vector[initial_orbital];
	      state_vector[initial_orbital] = storage;
	    }
	  else //Do nothing 
	    {
	      continue;
	    }
	}
          
      for(int q=0; q<10; q++)
	{
	  state_vector[q] = one_body_weight()*state_vector[q]*one_body_weight();
	}

      for(int m=0; m<=10;m++)
      	{
	  C_prime[N_e,m] = state_vector.at(m);
	}
    }
 
return 0;
}

float expon(double x) //Generates exponentially distributed random numbers
{
  double z=0;
  double exp_value=0;

do
  {
    z= rand_val(0);
  }while ((z==0 || z==1));

 exp_value = -x*log(z);
 return exp_value;

}

float rand_val(int seed)
{
  const long  a =      16807;  // Multiplier
  const long  m = 2147483647;  // Modulus
  const long  q =     127773;  // m div a
  const long  r =       2836;  // m mod a
  static long x;               // Random int value
  long        x_div_q;         // x divided by q
  long        x_mod_q;         // x modulo q
  long        x_new;           // New x value

  // Set the seed if argument is non-zero and then return zero
  if (seed > 0)
  {
    x = seed;
    return(0.0);
  }

// RNG using integer arithmetic
  x_div_q = x / q;
  x_mod_q = x % q;
  x_new = (a * x_mod_q) - (r * x_div_q);
  if (x_new > 0)
    x = x_new;
  else
    x = x_new + m;

  // Return a random value between 0.0 and 1.0
  return((double) x / m);
}

int poisson(double x) // Poisson distribution for number of steps
{
  int poi_value=0;
  double t_sum=0.0;

  while(1)
    {
      t_sum = t_sum + expon(x);
      if (t_sum >= 1.0) break;
      poi_value++;
    }

  return poi_value;
}

float one_body_weight() //Weight w for one body part of the hamiltonian 
{
  float E_1;
  E_1 = exp(0); // Currently set to 1.
  return E_1;
}

Here is the compile error I get..
Code:
mal@ubuntu:~/research$ g++ csmc.cc
csmc.cc: In function &#8216;int main()&#8217;:
csmc.cc:94: error: no match for &#8216;operator=&#8217; in &#8216;C_prime.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >](((long unsigned int)m)) = state_vector.std::vector<_Tp, _Alloc>::at [with _Tp = int, _Alloc = std::allocator<int>](((long unsigned int)m))&#8217;
/usr/include/c++/4.4/bits/vector.tcc:156: note: candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
 
Last edited:
right before return 0, you are assigning to C_print[N_e,m] = blah.

Check the specs on the STL vector. I don't believe you can do blah[x,y] subscripting on a vector.

I think you'll have to do
Code:
blah[x][y]
or
Code:
(blah[x])[y]
or something like that.
 
right before return 0, you are assigning to C_print[N_e,m] = blah.

Check the specs on the STL vector. I don't believe you can do blah[x,y] subscripting on a vector.

I think you'll have to do
Code:
blah[x][y]
or
Code:
(blah[x])[y]
or something like that.

Obviously. Duh...
:( I now feel like a retard. Thnks!
 
Status
Not open for further replies.
Back
Top