|
| olove05 (59) | |
| 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 ) { } | |
| helios (4790) | |
If I understand correctly what you're asking, Queue<int> queue; | |
| olove05 (59) | |
| 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}; | |
| helios (4790) | |
| You need a constructor like this: Queue(const int &); You pass the parameter to the constructor of Array<T>. | |
Last edited on | |
| olove05 (59) | |
| I can not have modification the class any other idea? | |
| helios (4790) | |
| There's no other way. Your current constructor takes no parameters. | |
| olove05 (59) | |
| 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; }; | |
| helios (4790) | |
| 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. | |
| olove05 (59) | |
| 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} | |
| helios (4790) | |||
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:
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 | |||
| olove05 (59) | |
| 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]â | |
| helios (4790) | |
| Sorry. I meant queue.enqueue(array[a]). | |
| olove05 (59) | |
| 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 | |
| olove05 (59) | |
| Hi every body I Have a questions I wonder If any body is there | |
| olove05 (59) | |
| [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 | |
| olove05 (59) | |
| HElloooooooo! | |
Last edited on | |
| Zhuge (324) | |
| 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. | |
| olove05 (59) | |
| WHERE I CAN FINE THE TAGS? | |
| Zhuge (324) | |
| Use [code]Your code[/code]. You can also click the # in the format area to the right of the input box. | |
Last edited on | |
| olove05 (59) | |
| 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: [1] [2]
Registered users can post in this forum.
