LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2007, 07:11 AM   #1
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Rep: Reputation: 15
An array matrix problem in a class


Hi all:

Say I have an matrix type defined as
Code:
typedef NRMatrix<float> Mat_SP
To define a matrix type the most obvious way is to declare like
Code:
Mat_SP mymat(300,300);
Now if I want this:
Code:
class myclass{
private:
    Mat_SP mymat;
public:
    inline myclass(){ mymat = Mat_SP(300, 300);}
}
Why this always gave me the error in runtime saying the "segmentation fault" please?

Is there a way of declaring the size of my matrix when constructing the object please?

Thanks in advance.
 
Old 12-06-2007, 07:56 AM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I'm not too knowledgable with C++ (as I'm assuming this is), but if you're trying to instantiate an object, shouldn't you drop in the "new" keyword somewhere?
Code:
Mat_SP mymat = new Mat_SP(300,300);
 
Old 12-06-2007, 07:59 AM   #3
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
No, it doesn't compile that way but I don't know why C++ doesn't work that way.
Here is the class of the NRMat:
Code:
template<class T>
class NRMat{
private:
    int nn;
    int mm;
    T **v;
public:
    NRMat();
    NRMat(int n, int m); // zero_based array
    NRMat(const T &a, int n, int m); // initialize to constant
    NRMat(const T *a, int n, int m);// initialize to array
    NRMat(const NRMat &rhs);//copy constructor
    NRMat & operator=(const NRMat &rhs);
    NRMat & operator=(const T &a);
    inline T* operator[](const int i);
    inline const T* operator[](const int i) const;
    inline int nrows() const;
    inline int ncols() const;
    ~NRMat();
};

Last edited by Asuralm; 12-06-2007 at 08:02 AM.
 
Old 12-06-2007, 08:57 AM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Okay - so, all of the public functions and constructors I see here are just being prototyped, like I would see in a header (.h) file. This may seem like a dumb question, but are you "#include"ing the file with the completely defined functions and constructors, or are you hand-writing them in yourself?
 
Old 12-06-2007, 09:09 AM   #5
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
yeah, sure I included everything properly. Actually what I really want to do is to declare a matrix field in the class definition. Then, I can define the size of the matrix when constructe the object. Obviously the matrix size can't be declared explicitly in class definition. But how to define the size of the matrix in construction ???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't pass 2d-array-class-pointer Bariton Programming 3 09-11-2007 08:30 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
double matrix array in c alaios Programming 3 09-15-2005 11:34 AM
(linking?) problem compiling very simple array class btb Programming 3 08-02-2005 03:16 PM
dynamic array in class help PTBmilo Programming 6 03-09-2003 02:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration