eillegal call of non-static member function error

Status
Not open for further replies.

lbduballstrs

Baseband Member
Messages
87
I'm writting this program for class and I can't get rid of these errors:
C:\Program Files\Microsoft Visual Studio\MyProjects\SparseMatrix\SparseSource.cpp(26) : error C2352: 'Matrix::getmatrix' : illegal call of non-static member function
b:\sparseheader.h(18) : see declaration of 'getmatrix'
C:\Program Files\Microsoft Visual Studio\MyProjects\SparseMatrix\SparseSource.cpp(27) : error C2352: 'Matrix::putmatrix' : illegal call of non-static member function
b:\sparseheader.h(19) : see declaration of 'putmatrix'

here is my code:
SparseHeade.h
#include<iostream>

using namespace std;

struct oneitem
{
int row, col;
float value;
};

oneitem sVariable;
const int MAXSIZE=225;

class Matrix
{
public:
void getmatrix (ifstream &);//reads the matrix from a file
void putmatrix (ofstream &);//write the matrix to a file

private:
oneitem data[MAXSIZE];
int rows,columns;//significant rows and columns

};
SparseSource.cpp
#include "b:\SparseHeader.h"//contains struct and class data
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream indata;
ofstream outdata;

indata.open("b:\\indata.txt");//disk file for input
if(!indata)
{
cout<<"Failed to open file indata.txt."<<endl;
return 1;
}//test fo opening indata.txt
outdata.open("b:\\outdata.txt");
if(!outdata)
{
cout<<"Failed to open file outdata.txt."<<endl;
return 1;
}//test for opening outdata.txt


Matrix::getmatrix(indata);
Matrix::putmatrix(outdata);


return 0;
}

void Matrix::getmatrix(ifstream & filedata)
{
filedata>>sVariable.row;
filedata>>sVariable.col;
while(filedata)
{
for( rows=0; rows<sVariable.row; rows++)
for(columns=0; columns<sVariable.col; columns++)
{
filedata>>sVariable.value;
data[rows+columns].value=sVariable.value;

}
}//end of while loop
}

void Matrix::putmatrix(ofstream & outfdata)
{
outfdata<<sVariable.row;
outfdata<<sVariable.col;
for(rows=0; rows<sVariable.row; rows++)
for(columns=0; columns<sVariable.col; columns++)
outfdata<<data[rows+columns].value;
}

If anyone knows the source of the errors I would greatly appreciate it.
Thanks
 
You need to create an instance of Matrix class and call with:

instance.getmatrix()

:: is the "Scope Resolution Operator" and is used only with static member functions or constants, and should not be used in this application. That is unless you intend to make those functions statics.
 
Also you should always create your own class constructors and destructors, you can never rely on what the compiler gives you. Even if they don't do anything just have them in there.

You should also put the class member functions definitions in their own implementation file .cpp or implement them right inside the class.
 
Status
Not open for further replies.
Back
Top Bottom