Re: << run time error
On Sep 20, 4:49 pm, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
deepakvs...@gmail.com wrote:
I'm writing a program for sparse matrix dll. Here i've overloaded <<
operator (friend). But when i run the program i get run time error.
My class structure is like this:
class _declspec(dllexport) CSparse {
public:
......
//Display the matrix
friend std::ostream & operator<< (std::ostream &, const CSparse &);
//Input the matrix
friend std::istream & operator>> (std::istream &, CSparse &);
.....
};
You are:
- using friend functions and IOStreams
- exporting classes from DLLs
- not providing the exact errorcode
and not even providing the sourcecode, just a snippet of it. I'd suggest you
first reduce the code to a minimal example that demonstrates the problem.
Chances are that you will find the error yourself on the way. Otherwise,
post that example here so people can try and reproduce the problem
themselves.
Uli
I get a abnormal program termination error and i suppose it doesn't
show any error code.
I've problem only with friend functions.
Here's the code which is producing error:
//Display the matrix
std::ostream & operator<< (std::ostream &out, const CSparse &Sparse)
{
for(int i = 0; i < Sparse.m_nMaxRows; i++) {
Node *pRowLead = Sparse.m_pRowHead[i];
int Col = 0;
while(0 != pRowLead) {
if(pRowLead->nCol == Col) {
out<<pRowLead->fWeight<<" ";
pRowLead = pRowLead->pNextCol;
} else {
out<<'0'<<" ";
}
Col++;
}
if(Col < Sparse.m_nMaxColumns) {
while(Col < Sparse.m_nMaxColumns){
out<<0<<" ";
Col++;
}
}
out<<"\n";
}
return out;
}
//Input the matrix
std::istream & operator>> (std::istream &in, CSparse &Sparse) {
float fWeight = 0.0f;
int nRow, nCol;
int nElementCount = 0;
bool bFlag = false, bContinue = false, bExists = false;
do {
bContinue = false;
bExists = false;
do {
bFlag = false;
std::cout<<"Enter row number:";
std::cin>>nRow;
nRow--;
if(nRow < 0 || nRow >= Sparse.m_nMaxRows) {
bFlag = true;
}
if(true == bFlag) {
std::cout<<"Invalid row number!!\n";
}
}while(true == bFlag);
do {
bFlag = false;
std::cout<<"Enter col number:";
std::cin>>nCol;
nCol--;
if(nCol < 0 || nCol >= Sparse.m_nMaxColumns) {
bFlag = true;
}
if(true == bFlag) {
std::cout<<"Invalid column number!!\n";
}
}while(true == bFlag);
std::cout<<"Enter weight:";
std::cin>>fWeight;
if(0 != fWeight) {
Node *pRowLead = Sparse.m_pRowHead[nRow];
Node *pColLead = Sparse.m_pColumnHead[nCol];
while(0 != pRowLead) {
if(pRowLead->nCol == nCol) {
bExists = true;
break;
}
pRowLead = pRowLead->pNextCol;
}
pRowLead = Sparse.m_pRowHead[nRow];
if(false == bExists) {
Node *pTemp = new Node(nRow, nCol, fWeight);
if(0 == pRowLead) {
Sparse.m_pRowHead[nRow] = pTemp;
nElementCount++;
} else {
if(pRowLead->nCol > nCol) {
pTemp->pNextCol = Sparse.m_pRowHead[nRow];
Sparse.m_pRowHead[nRow] = pTemp;
nElementCount++;
} else {
Node *pRowLeadPrev = NULL;
while(0 != pRowLead) {
if(pRowLead->nCol > nCol) {
break;
}
pRowLeadPrev = pRowLead;
pRowLead = pRowLead->pNextCol;
}
pTemp->pNextCol = pRowLeadPrev->pNextCol;
pRowLeadPrev->pNextCol = pTemp;
nElementCount++;
}
}
if(0 == pColLead) {
Sparse.m_pColumnHead[nCol] = pTemp;
} else {
if(pColLead->nRow > nRow) {
pTemp->pNextRow = Sparse.m_pColumnHead[nCol];
Sparse.m_pColumnHead[nCol] = pTemp;
} else {
Node *pColLeadPrev = NULL;
while(0 != pColLead) {
if(pColLead->nRow > nRow) {
break;
}
pColLeadPrev = pColLead;
pColLead = pColLead->pNextRow;
}
pTemp->pNextRow = pColLeadPrev->pNextRow;
pColLeadPrev->pNextRow = pTemp;
}
}
} else {
std::cout<<"["<<nRow+1<<"]["<<nCol+1<<"] element exists!!\n";
}
}
if(nElementCount < (Sparse.m_nMaxColumns * Sparse.m_nMaxRows)) {
std::cout<<"\nAdd another element (0 - Stop, 1 - Continue):";
std::cin>>bContinue;
}
}while(true == bContinue);
return in;
}