Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Segmentation fault (segfault) c++
Author Message
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #1
Segmentation fault (segfault) c++
Hey ljudi pozz,
mislim da naslov dosta govori ali evo pokusat cu objasniti,
vjezbao sam malo OOP i kada sam pokusao dinamički alocirati prostor preko operatora new javlja mi navedenu gresku, naime program se fino iskompailira ako itko moze pomoći hvala. Palac-gore


Edit : Evo i kod

Code:
#include <iostream>
using namespace std;

class matrix
{
    private:
        int rows;
        int cols;
        float **el;
    public:
        matrix()
        {
            rows = cols = 3;

            *el = new float[rows];
            for(int i=0; i<rows;++i)
                el[i] = new float[cols];

            *el = new float[rows];
            for(int i=0;i<rows;i++)
                el[i] = new float[cols];
            for(int i=0;i<rows; i++)
               for(int j=0; j<cols; j++)
                el[i][j] = 0;
        }
        void ispis()
        {
            for(int i=0;i<rows; i++)
            {
                for(int j=0; j<cols; j++)
                    cout << "\t" << el[i][j];
                cout << endl;
            }

        }
};

int main()
{
    matrix m1;
    m1.ispis();
    return 0;
}

"When all else fails, read the manual."
(This post was last modified: 26-11-2010 01:40 PM by danielvast.)
26-11-2010 11:39 AM
Find all posts by this user Quote this message in a reply
kecko Offline
Forumaš
***

Posts: 645
Joined: Nov 2009
Reputation: 20
Post: #2
RE: Segmentation fault (segfault) c++
nisi inicijalizirao matrix...
probaj vako, ostalo nisam gledo, ovo sam odmah vidio
Code:
int main()
{
    matrix m1=new matrix();
    m1.ispis();
    return 0;
}
26-11-2010 03:00 PM
Find all posts by this user Quote this message in a reply
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #3
RE: Segmentation fault (segfault) c++
Malo sam morao pročačkati ovako:
Code:
matrix *m1=new matrix;
    m1->ispis();
Hvala prijatelju radi sada super, neznak kako da ti se odužim hvala još jednom spasio si me. He he

"When all else fails, read the manual."
26-11-2010 03:13 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)