Hi all,
I am new in this forum and a novice in c++ programming in general, so please bear with me if my question seems stupid or my information is incomplete.
I am working on a project in which I have created the class stdModel. This class has a header and a .C file (I compile using a makefile on a linux system).
The constructor of the class reads in data matrices from a given datafile, these matrices are made as vectors of vectors. I can print these matrices to the screen from the constructor, but when I try to use them in the function called weightedSum, I get the error "Segmentation fault (core dumped)". I can work with the data from the constructor that is only single numbers (int), but the matrices do not work. I have tried reading them differently using push_back, but that yields the error message already when I try printing to the screen from the constructor.
A very long explanation, but I hope someone can help me.
Thank you in advance.
The code in stdModel.C is the following
/**
* Implementation of the class stdModel
*
*/
#include <stdModel.h>
using namespace std;
stdModel::stdModel( char* fname )
/**
* Constructor of class MyModel
*/
{
n = 0;
demand = 0;
noOfBreakpoints = 0;
try
{
// Construct input file object
ifstream InFile( fname );
if ( ! InFile ) throw myErrHandler("Could not open input file");
// Read number of suppliers, number of breakpoints and the total demand
int someInt, anyInt;
InFile >> n;
InFile >> someInt;
InFile >> anyInt;
if ( ! InFile ) throw myErrHandler( "Could not read number of suppliers" );
noOfBreakpoints = someInt;
demand = anyInt;
vector<vector<int> > breakpoints(n);
vector<vector<double> > c(n, vector<double>(noOfBreakpoints));
vector<vector<double> > f(n, vector<double>(noOfBreakpoints));
vector<vector<int> > e(n, vector<int>(noOfBreakpoints));
//Read breakpoints from the input file
for ( int i=0; i<n; i++ ) {
breakpoints.push_back(vector<int> (noOfBreakpoints));
for ( int j=0; j<noOfBreakpoints; j++ ) {
InFile >> anyInt;
if ( !InFile) throw myErrHandler( "Could not read breakpoints" );
breakpoints[j].push_back(anyInt);
}
}
/* cout << "prints the breakpoints matrix" << endl;
for( int i=0; i<n; i++){
for( int j=0; j<noOfBreakpoints; j++){
cout << breakpoints[i][j] << " ";
}
cout << endl;
}
*/
// Read fixed cost from input file
for ( int i=0; i < n; i++ ) {
for ( int j=0; j<noOfBreakpoints; j++ ) {
InFile >> anyInt;
if ( !InFile ) throw myErrHandler( "Could not fixed cost" );
f[i][j] = anyInt;
}
}
cout << "prints the fixed cost matrix" << endl;
for( int i=0; i<n; i++){
for( int j=0; j<noOfBreakpoints; j++){
cout << f[i][j] << " ";
}
cout << endl;
}
// Read variable cost
double anyNum;
for ( int i=0; i < n; i++ ) {
for (int j=0; j<noOfBreakpoints; j++ ) {
InFile >> anyNum;
if ( !InFile ) throw myErrHandler( "Could not read variable cost" );
c[i][j] = anyNum;
cout << c[i][j] << " ";
}
cout << endl;
}
// Read sustainability coefficients
for ( int i=0; i<n; i++) {
for (int j=0; j<noOfBreakpoints; j++) {
InFile >> anyInt;
if ( !InFile ) throw myErrHandler( "Could not read sustainability coefficients" );
e[i][j] = anyInt;
}
}
}
catch (myErrHandler& e)
{
e.showMsg();
}
}
void stdModel::weightedSum(pair<double,double> zUL, pair<double,double> zLR, vector< pair <double,double> >& repList)
/*
* Implements the weighted sum method on the model
*/
{
try
{
// Calculate the lambda values
double lambda1 = zUL.second-zLR.second;
double lambda2 = zLR.first-zUL.first;
cout << "n er " << n << endl;
cout << "noOfBreakpoints er " << noOfBreakpoints << endl;
cout << "lambda1 er " << lambda1 << " og lambda2 er " << lambda2 << endl;
// Create the coefficient matrices cWS and fWS for the weighted sum objective
vector<vector<double> > cWS(n, vector<double>(noOfBreakpoints));
for( int i=0; i<n; i++)
{
for( int j=0; j<noOfBreakpoints; j++)
{
cWS[i][j] = lambda1*c[i][j]+lambda2*e[i][j]; //this line is what gives me the error message!
}
}
}
catch( myErrHandler& e)
{
e.showMsg();
}
}