473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A problem when creating a matris

Hello Experts!!

I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};
Jul 23 '05 #1
3 1752
Tony Johansson wrote:
I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}
return *this;
}
private:
int size;
int* array;
Your class uses dynamic memory. It is at this point missing a proper
copy-constructor and a proper destructor. Please read about "the Rule
of Three" and correct your code accordingly.
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
'matris' is a pointer with indeterminate value at this point. You're
trying to dereference it. That's the reason for the crash (krasch).
What you should do, probably, is

matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


You probably shouldn't attempt dynamic memory management without some
better understanding of pointers and operations on them.

V
Jul 23 '05 #2
Hello!!

I know I must have a copy constructor and a destructor but I just cut out
that to make it easier for you to understand the code.
//Tony

"Victor Bazarov" <v.********@comAcast.net> skrev i meddelandet
news:Le*******************@newsread1.mlpsca01.us.t o.verio.net...
Tony Johansson wrote:
I have two small classes called Intvektor and Matris shown below and a
main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of
Intvektor in this way I will create a matris. For example an matris of x
rows and y columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class
Matris.
The problem is something with pointer. I assume that I must have a
pointer to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}
return *this;
}
private:
int size;
int* array;


Your class uses dynamic memory. It is at this point missing a proper
copy-constructor and a proper destructor. Please read about "the Rule
of Three" and correct your code accordingly.
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************


'matris' is a pointer with indeterminate value at this point. You're
trying to dereference it. That's the reason for the crash (krasch).
What you should do, probably, is

matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


You probably shouldn't attempt dynamic memory management without some
better understanding of pointers and operations on them.

V

Jul 23 '05 #3
Tony Johansson wrote:
Hello Experts!!

I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.

I hope you can tell me how I should declare matris variable to be able to
crete a matris.

//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
*matris = new Intvektor[0];
}

Matris(int rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};


---- Matrix.h ----

// Matrix.h
#include <vector>
#include <iostream>

// a vector of int's
typedef std::vector< int > Intvektor;

// a vector of Intvektor's (i.e. a 2 dim matrix of int's)
typedef std::vector< Intvektor > Int2vektor;
// class Matrix derived from a vector of vectors of int.
// all of the features of 'vector' are available to Matrix.
class Matrix : public Int2vektor
{
public:
Matrix() : Int2vektor()
{
}

Matrix(int row, int col) : Int2vektor(row, Intvektor(col))
{
}

Matrix(const Matrix& m) : Int2vektor(m)
{
}

// dump my contents to 'os' for debugging
std::ostream& dump(std::ostream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;

for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}

os << std::endl;

return os;
}
};

---- end of Matrix.h ----

---- MatrixTest.cpp ----

// Matrix.cpp
#include "Matrix.h"

int main()
{
Matrix a(2,2);

// dump the initial contents of 'a'
a.dump(std::cout, "'a' initial contents");

int v = 0;

// put some values into 'a'
for (int row = 0; row < a.size(); row++)
for (int col = 0; col < a[row].size(); col++)
a[row][col] = v++;

// dump the new contents of 'a'
a.dump(std::cout, "'a' with values added");

// make 'b' as a copy of 'a'
Matrix b(a);

// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'a'");

// erase 'a'
a.clear();

// demonstrate that the copy-constructor
// actually copied the data from 'a' to 'b'
a.dump(std::cout, "'a' after a.clear()");
b.dump(std::cout, "'b' after a.clear()");

// copy 'b' to 'a'
a = b;
a.dump(std::cout, "'a' after 'a = b'");

return 0;
}

---- end of MatrixTest.cpp ----

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joonas Paalasmaa | last post by:
Hi, When compiling Sketch's streamfilter C extension the errors below are raised during linking. What could cause the errors? (Python 2.3, MinGw 1.1 with GCC 2.95.3-6, Windows 98) Here are...
1
by: Chris Dunaway | last post by:
When deciding to use a structure or a class, what is the general "rule of thumb"? When you're creating your data model, what factors dictate that you use a class instead of a structure? ...
22
by: Bradley | last post by:
Has anyone else noticed this problem? I converted the back-end to A2000 and the performance problem was fixed. We supply a 97 and 2000 version of our software so we kept the backend in A97 to make...
6
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the...
0
by: Ravi Ambros Wallau | last post by:
Hi: I've created a custom control - a grid that uses Infragistics to display some filters, the grid itself, and some buttons. Well, when using this control directly on WebForm, everything works...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
4
by: Bit byte | last post by:
I have a project that I normally build (without problems) from the DevStudio IDE. However, I have embarked on automating all my builds (this test project being one of several). The project...
9
by: Joe Penora | last post by:
Hi All, How to add the HTML script when creating new ASPX page. After it is done I may need to add some Meta tags etc... but the html script is not there. Is there an option in the ASP.NET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.