473,698 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class array ? (2 dimensional)

eb
Hello,

This *should* be simple, but I'm not knowledgeable enough so as to get it by
myself.

Suppose I want a double array of a custom class (say MyTile, to tile a
square).

How would I declare it in MyTiledSquare.h ?

I did not succeed with "MyTile **myTiles ;"
The trick is : I want the tiling size (the number of tiles per row / lines)
to depend on a variable. Therefore, I don't want to fix the array size
there.
Then, how would I initialise it in MyTiledSquare.c pp ?
The array size is dependent on a variable which is in the MyTiledSquare
class.

In the end, I would like to access an element with MyTile[i][j]

Thanks for any clues.
Jun 11 '06 #1
6 1887
eb wrote:
Hello,

This *should* be simple, but I'm not knowledgeable enough so as to get it by
myself.

Suppose I want a double array of a custom class (say MyTile, to tile a
square).

How would I declare it in MyTiledSquare.h ?

I did not succeed with "MyTile **myTiles ;"
The trick is : I want the tiling size (the number of tiles per row / lines)
to depend on a variable. Therefore, I don't want to fix the array size
there.
Then, how would I initialise it in MyTiledSquare.c pp ?
The array size is dependent on a variable which is in the MyTiledSquare
class.

In the end, I would like to access an element with MyTile[i][j]

Thanks for any clues.


You'll save yourself a lot of headache if you use std::vector. Example:

unsigned rows = 25, cols = 80 ;
std::vector< std::vector<T> > v(rows, std::vector<T>( cols)) ;

--
Alan Johnson

Jun 11 '06 #2
eb wrote:
Hello,

This *should* be simple, but I'm not knowledgeable enough so as to get it by
myself.

Suppose I want a double array of a custom class (say MyTile, to tile a
square).

How would I declare it in MyTiledSquare.h ?

I did not succeed with "MyTile **myTiles ;"
The trick is : I want the tiling size (the number of tiles per row / lines)
to depend on a variable. Therefore, I don't want to fix the array size
there.
Then, how would I initialise it in MyTiledSquare.c pp ?
The array size is dependent on a variable which is in the MyTiledSquare
class.

In the end, I would like to access an element with MyTile[i][j]

Thanks for any clues.


I agree with Alan Johnson, in that using vector<vector<T > > would be
the better approach.
However, if you need a **T type, or if you need contiguous memory for
the 2D array, you can consider using one of the following methods:

(Allocate2DArra y function that returns type **T with contiguous 2D
memory)
http://code.axter.com/allocate2darray_t.h

(Generic wrapper class that creates contiguous 2D memory)
http://code.axter.com/dynamic_2d_array.h

Jun 11 '06 #3
eb
Thx Alan,

Not being a programmer, I was not aware of the existence of Vector<>
I'll try that

Alan Johnson wrote:
eb wrote:
Hello,

This *should* be simple, but I'm not knowledgeable enough so as to get it
by myself.

Suppose I want a double array of a custom class (say MyTile, to tile a
square).

How would I declare it in MyTiledSquare.h ?

I did not succeed with "MyTile **myTiles ;"
The trick is : I want the tiling size (the number of tiles per row /
lines) to depend on a variable. Therefore, I don't want to fix the array
size there.
Then, how would I initialise it in MyTiledSquare.c pp ?
The array size is dependent on a variable which is in the MyTiledSquare
class.

In the end, I would like to access an element with MyTile[i][j]

Thanks for any clues.


You'll save yourself a lot of headache if you use std::vector. Example:

unsigned rows = 25, cols = 80 ;
std::vector< std::vector<T> > v(rows, std::vector<T>( cols)) ;


Jun 11 '06 #4
eb
Alan Johnson wrote:


You'll save yourself a lot of headache if you use std::vector. Example:

unsigned rows = 25, cols = 80 ;
std::vector< std::vector<T> > v(rows, std::vector<T>( cols)) ;


Well, it's not that simple to me ...
I'm using a QT class as T : QCanvasLine

I started with a simple version :
unsigned rows = 25
std::vector< QCanvasLine > v(rows) ;

Here is the error message.

/home/eb/Packages/qgo.new/src/board.cpp: In member function `void
Board::drawGatt er2()':

/home/eb/Packages/qgo.new/src/board.cpp:344: warning: unused variable 'cols'
/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include/g++-v3/bits/stl_vector.h: In
constructor `std::vector<_T p, _Alloc>::vector (size_t) [with _Tp =
QCanvasLine, _Alloc = std::allocator< QCanvasLine>]':

/home/eb/Packages/qgo.new/src/board.cpp:345: instantiated from here

/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include/g++-v3/bits/stl_vector.h:20 7:
error: no matching function for call to `QCanvasLine::Q CanvasLine()'

/usr/qt/3/include/qcanvas.h:685: note: candidates are:
QCanvasLine::QC anvasLine(const QCanvasLine&)
/usr/qt/3/include/qcanvas.h:687: note: QCanvasLine::QC anvasLine(QCanv as*)
However, when try to use a 'candidate'
(say QCanvasLine::QC anvasLine(canva s), where canvas is already declared ),
things don't get better :

/home/eb/Packages/qgo.new/src/board.cpp: In member function `void
Board::drawGatt er2()':
/home/eb/Packages/qgo.new/src/board.cpp:341: error: conversion from
`QCanvasLine*' to non-scalar type `QCanvasLine' requested
/home/eb/Packages/qgo.new/src/board.cpp:345: error: `Board::canvas' cannot
appear in a constant-expression
/home/eb/Packages/qgo.new/src/board.cpp:345: error: template argument 1 is
invalid
/home/eb/Packages/qgo.new/src/board.cpp:345: error: template argument 2 is
invalid
/home/eb/Packages/qgo.new/src/board.cpp:345: error: invalid type in
declaration before '(' token

Aby clue ?
Jun 11 '06 #5
eb wrote:
Alan Johnson wrote:

You'll save yourself a lot of headache if you use std::vector. Example:

unsigned rows = 25, cols = 80 ;
std::vector< std::vector<T> > v(rows, std::vector<T>( cols)) ;


Well, it's not that simple to me ...
I'm using a QT class as T : QCanvasLine

I started with a simple version :
unsigned rows = 25
std::vector< QCanvasLine > v(rows) ;


There are two different forms of vector's constructor that are used in
the line of code I proposed. The first is:
vector<T> v(num) ;

This makes a vector containing num objects of type T, and uses T's
default constructor to initialize them. An alternative is:
vector<T> v(num, object) ;

This creates a vector with num copies of object. Looking at the
documentation for QCanvasLine (disclaimer: I don't know Qt), it doesn't
have a default constructor, so the first form won't work. However,
something like the following should (assuming canvas has been properly
defined):

size_t cols = 80 ;
std::vector<QCa nvasLine> vcols(cols, QCanvasLine(can vas)) ;

That will make 80 copies of the temporary object created by
QCanvasLine(can vas).

Now, you want two dimensions, so what you want is several copies of the
vector we just defined:

size_t rows = 25 ;
std::vector< std::vector<QCa nvasLine> > lines(rows, vcols) ;

Or, if you want to do it all together:

size_t rows = 25, cols = 80 ;
std::vector< std::vector<QCa nvasLine> > lines(rows,
std::vector<QCa nvasLine>(cols, QCanvasLine(can vas))) ;

--
Alan Johnson
Jun 11 '06 #6
eb
Alan Johnson wrote:


Or, if you want to do it all together:

size_t rows = 25, cols = 80 ;
std::vector< std::vector<QCa nvasLine> > lines(rows,
std::vector<QCa nvasLine>(cols, QCanvasLine(can vas))) ;


Close ... What dod the trick was :

std::vector< std::vector<QCa nvasLine> > HGatter (board_size,
std::vector<QCa nvasLine>(board _size, QCanvasLine::QC anvasLine(canva s))) ;

Now I can compile.

Unfortunately, the code does not work yet
(I have then the initialisation of each HGatter line, but they would not
show :-( )

int i,j;

for (i=0; i<board_size; i++)
for (j=0; j<board_size; j++)
{
HGatter[i][j].setPoints(offs etX + square_size * i, offsetY,
offsetX + square_size * i, offsetY + board_pixel_siz e);
HGatter[i][j].show();
}

Thanks anyway.
Jun 12 '06 #7

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

Similar topics

1
6029
by: Mick | last post by:
Hello, I know C++ but am new to C# so bear with me. I want to create an array of my own class. Something like this: /////////////////////////////////////// class CLoginInfo { public string name; public string id; public string pw;
0
978
by: Opa | last post by:
Hi, I am using the CF.NET which doesn't support serialization, so I am trying to do it myself. I have a class array which I am trying to store to a file using a binary writer. Is there a way of doing this without looping through each element in the class array?
4
1617
by: Marc Castrechini | last post by:
Forgive any bad terms. New to .Net development My business layer has something similiar to this class Accoun private m_Notes as Note( ' Get Mehod .. end clas
6
14307
by: Ant | last post by:
Hi, something I imagine would be simple but... How can you create a class array? e.g. MyClass mc = new MyClass(); ????????? This obviously doesn't work, but how is it done? Thanks for any advice in advance. Ant
1
2756
by: maradona | last post by:
Hi!! I want to use an array into a function of an object.I decided to declare the array in the class and to initialize its values with the constructor.I wrote something like this: class my_class{ public: ....... int my_array; ...... } my_class::my_class() {
0
1136
by: Matthew87 | last post by:
Hi all I'm having a bit of trouble completeing my coursework basically so far i have created Citenary class with support methods for a private array. The problem is i can add objects to the array through a instance of the class in the main form but im having trouble retreving the data from a combobox. I have the values Name, Passport Number, Issuedby and Expires that are added to the array. The value name is passed to the combobox when a...
1
983
by: balurajeev | last post by:
i'm balu working as trainee in firm .now im in big trouble while usin g class array. i dont know how to read values uding class as array? please help me
5
3243
by: Ben | last post by:
Does anyone have any example code showing how to sort a datagrid that is bound to a custom class array? My code calls a web method that returns an array of type LeadListDS. LeadListDS consists of string, DateTime, and boolean datatypes. This returned array is then bound to the datagrid and seems to work fine I just need to figure out this sorting issue. Thanks Ben
1
2445
by: Peter van der Zee | last post by:
L.S., I'm want to fill a Listbox with class Array "records" based on certain criteria. The idea is to be able to select one "record" at the time, modify the data, and display this "changed" record in the listbox again. On each line of the listbox I want to display various class items. (columns??)
0
8683
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
9170
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...
1
8901
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
8871
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
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.