class inside class

Pages: 12
how to declare in main a object of class that has within other class with variable member that point to dynamic array

////////The Array.h
template <class DataType>
class Array
{
public:
Array( int size );

private:
T *ele;
};

///////The Array.cpp
Array<T>::Array( int size )
{
if ( size < 1 ) {
capacity = 1;

}
else {
capacity = size;

}

ele = new T [capacity];
}
----------------------------------------------------------------------------------
///////////Q.h


#include "Array.h"


template <class T>
class Queue
{
public:
Queue( );
void enqueue( T ele );
private:
Array<T> ele;
int front;
int back;
};

///////Q.cpp

template <class T>
Queue<T>::Queue( )
: ele( 2 ), front( -1 ), back( -1 )
{
}

template <class T>
void Queue<T>::enqueue( T ele )
{
}


If I understand correctly what you're asking, Queue<int> queue;
I have this allready, is how to

do this Queue<int> eleArQueue []= { 5, 1, 4, 6, 4, 5};

//int L []= { 5, 11, 34, 67, 43, 55};
You need a constructor like this:
Queue(const int &);
You pass the parameter to the constructor of Array<T>.
Last edited on
I can not have modification the class

any other idea?
There's no other way. Your current constructor takes no parameters.
How abou this , I did not post the all code, this is the cosntructor, see below the costructor what I did in main and i have errors


template <class T>
void Queue<T>::enqueue( T ele )
{
if ( back + 1 == front || ( back == ele.length( ) - 1 && !front ) ) {

ele.changeS( ele.length( ) << 1 );

// if front end was last part of array, readjust

if ( back < front ) {

int i = ele.length( ) - 1;

for ( int j = ((i + 1) >> 1) - 1; j >= front; i--, j-- )

ele[ i ] = ele[ j ];

front = i + 1;
}
}
-------------------------------------------------------------
struct Mylist
{
int lista[];
int n ;
};

using namespace std;


int main()
{

Queue<int> elementsArQueue;

Mylist LisObj;


LisObj.lista = {5, 11, 34, 67, 43, 55};

//P.cpp:27: error: expected primary-expression before â{â token
//P.cpp:27: error: expected `;' before â{â token


//Array<int> ElemLis( 5 );

return 0;

};








The {} syntax can only be used during initialization.
Sorry, but there's just no other way.

Why do you need to do this? Maybe there's something else you can try.
I have to be able of give those element to the queue array and swap the nth element to the front
of the queue

example
n=3
q{1,2,3,4,5}

the result q{4,1,2,3,5}


That wasn't what you were doing here:
Queue<int> eleArQueue []= { 5, 1, 4, 6, 4, 5};
If you had the right constructor, this would have created six different queues, possibly of different initial lengths.

What you want to do is completely impossible. There's no way to do it other than by queuing each item one by one, or this:
1
2
3
4
5
{
	int array[]={1,2,3,4,5,6};
	for (int a=0;a<6;a++)
		queue.enqueue(array);
}


As an aside, the next revision to the language will allow constructing vectors like this:
std::vector<int> v={1,2,3,4,5,6};
And then you could iterate over the vector using v.size(). Pretty convenient, huh?
Last edited on
Ok, I did you suggestion, I can not understand how to fix those errors

P.cpp:28: error: invalid conversion from âint*â to âintâ
P.cpp:28: error: initializing argument 1 of âvoid Queue<T>::enqueue(T) [with T = int]â
Sorry. I meant queue.enqueue(array[a]).
thanks Helios

I have a questions. How to pass by argument a queue array to a function, it has to accept Queue
list by argument

Hi every body I Have a questions I wonder If any body is there
[TEX]Any idea for what to do in these case[/TEXT]

using namespace std;

// I declare a function template fro display the queue
template <T>
void Disply(const DataType elem[] )
{

for( int i = 0; i<6; i++)

cout<< "The elements of the Queue are:\n", i , elem[i]<<endl;

}

int main()
{

Queue<int> elementsArQueue;

int array[]={1,2,3,4,5,6};

for (int a=0;a<6;a++)

queue.enqueue(array);

Display(elementsArQueue.);// give to me some errors like

//P.cpp: In function âint main()â:
P.cpp:48: error: no matching function for call to âDisplay(Queue<int>&)â



Last edited on
HElloooooooo!
Last edited on
Please post using code tags...

And the error means you are trying to call a function Display() that isn't defined with a prototype or a body anywhere the compiler can see.
WHERE I CAN FINE THE TAGS?
Use [code]Your code[/code].

You can also click the # in the format area to the right of the input box.
Last edited on
OK


Now, I have to do a function that receive a queue with the code above .

my questions is, How I can move to the front of the queue the nth element receive by argument

example n = 2

foo(queue,n)

queue = 2,3,4,5, 6

after foo operations is
4,2,3,5,6 // move to the front the nth elements

ANY SUGGESTION ?
Pages: 12