473,769 Members | 5,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1783
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.********@com Acast.net> skrev i meddelandet
news:Le******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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::ostre am& 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::cou t, "'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::cou t, "'a' with values added");

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

// dump the contents of 'b'
b.dump(std::cou t, "'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::cou t, "'a' after a.clear()");
b.dump(std::cou t, "'b' after a.clear()");

// copy 'b' to 'a'
a = b;
a.dump(std::cou t, "'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
4931
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 the occurrences of FilterType that may be relevant: ------- C:\sketch\sketch-0.7.12\Filter\filterobj.c: 949: PyTypeObject FilterType = {
1
6782
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? Structures are more lightweight than classes but for example if you're only going to create a single instance (for whatever reason), it seems that the added "weight" of a class would be negligible. If creating many such instances, it might be...
22
2358
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 upgrading simple for users. We've done it like that for years but a new client has been having severe performance issues... solved by converting the backend to 2000. -- regards, Bradley
6
13730
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 error (seems to be happening when I try creating a directory) If dirmgr.Exists("s:\blah\" & txt_name.Text) Then lblerror.Text = lblerror.Text & "Unable to build physical path. " &
0
2135
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 really fine. No problems at all. But, when this control is placed inside a template of an Infragistics UltraWebTab control, somethings stranges happens: 1. When the WebForm is opened, an "Error Creating Control" message is displayed (for container...
3
5075
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). I changed the data type for the structure that contains the XML data from the default "String" to "xml.xmldocument" to enable easy filling of the data. my client code creates an XML document class, fills the data using standard xml dom...
5
3646
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 New System.Diagnostics.Process 'p.Start(MDEPDirStr & "macrun.exe", sPPTOut) p.Start("C:\WINDOWS\SYSTEM32\CALC.EXE") 'p.Start("C:\WINDOWS\SYSTEM32\macrun.exe", sPPTOut)
4
5006
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 creates a DLL. I am able to build the project without any probs in the IDE, however - when I build the project from the command line (using the same options shown in the 'Command line' node in the 'Project Settings' dialog box), I get the following...
9
5997
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 that WHEN creating a new aspx page, then it will generate that basic HTML scrip as well. Regards,
0
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.