Method for a class with matrices

srbbff (7)
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();
}
}

Last edited on
Zhuge (2880)
Have you tried debugging it to see when the fault occurs?
srbbff (7)
I am only using c++ for one project and therefore I have not got a proper debugger, so far I have simply been using print statements to check how far the code can run (I know this is not how it should be done)...
srbbff (7)
It compiles fine, but when I run the code on a data file the fault occurs when accessing elements from the matrices defined in the constructor within the weightedSum function.
Peter87 (3691)
I think breakpoints[j].push_back(anyInt); should be breakpoints[i].push_back(anyInt);

You have given breakpoints[i] an initial size of noOfBreakpoints and then you add noOfBreakpoints more elements with push_back so breakpoints[i] will contain 2*noOfBreakpoints elements. Is that your intention?
srbbff (7)
The intention is to create a matrix with n rows (hence the outer for-loop iterating from i=0, i<n) and then for every row vector add noOfBreakpoints entries (the inner for-loop), and thereby ending up with noOfBreakpoints columns...
Topic archived. No new replies allowed.