0

Hey guys i am i am new to C++ and through to a project i have in the university i am having some difficult times . More spesificaly : i ve created a code for Lists and Queues (Lists name = Chain , Queues name = Queue , Product is a struct that has basicly Chains fields) [btw i ve used Sahnis book (data structures) if anyone knows it. I am stuck here :

int k=4;
Queue<Chain<Product>*>* x = new Queue<Chain<Product>*> [k];
for(int i=1;i<k;i++)
{
   x[i] = new Queue<Chain<Product>*> [i+1];
}

on the loop it throws me error : Invalid conversion from Queue*>* to int

Any idea?

1
  • Using a vector (or even a normal array) instead of pointer array will help too. Commented May 3, 2012 at 20:13

2 Answers 2

2

It should be

for(int i=0;i<k;i++)   // first index is 0
{
   x[i] = Queue<Chain<Product>*>();
}

because

Queue<Chain<Product>*>* x = new Queue<Chain<Product>*> [k];

creates an array of Queue<Chain<Product>*> objects, not pointers.

Or if you want a 2-d array, you use:

Queue<Chain<Product>*>** x = new Queue<Chain<Product>*> * [k];
for(int 0=1;i<k;i++)
{
   x[i] = new Queue<Chain<Product>*> [i+1];
}

To simplify, you're basically attempting the following:

int* x = new int[4];
for ( int i = 0 ; i < 4 ; i++ )
   x[i] = new int[i];

which is obviously wrong.

Sign up to request clarification or add additional context in comments.

7 Comments

Btw, I don't remember if it's doable in C++ (I work in C# and Java mainly) but can't Queue<Chain<Product>*> have a static function .operator[int x]? It's obviously not the case here, but just asking.
@Shingetsu yes, it's possible to overload operators, but I don't see the point of having a static operator[].
I mean is it possible to overload an operator to be static? And if it could exist it might be used to retrieve an decoded version of the class from a certain file location or similar, those are details.
@Shingetsu I don't think it's possible, and again, it wouldn't make sense.
Now the code compiles , but when i run it windows pop a message that says : filename.exe stoped executing . Note that the same thing was happening without the for loop!
|
1

In the line x[i] = new Queue<Chain<Product>*> [i+1] he [i+1] is wrong.
Why? Well you're creating a new object new keyword. and .operator[int x] is used in arrays. In that line you are saying it should be a new Array of size i+1 of type Queue<Chain<Product>*> which is faulty. Instead use x[i] = Queue<Chain<Product>*>();

So end code is:

for(int i=0;i<k;i++)//because indexes begin at 0, not 1.
{
  x[i] = Queue<Chain<Product>*>()
}

Note* to see a simplified version of your mistake, see other guy's post (I won't copy code around - wastes space).

3 Comments

Thank you for your help . when i run it it compliles perfect , however windows pop up an error when i run it that says that filename.exe stoped exececuting. Without the for loop it pops the same thing wich means that i still havent saved space for my array. Simply with my old code id didnt compile cause i had several mistakes but if i hadnt the for loop , the result is the same one with your code
@Tryme_34 it's not linked with this. Stopped executing doesnt always mean run out of memory, my guess is it's an error later in the code. Post a new question with full source.
yeah i will post tomorrow , but keep in mind that my code compiles and run okay without the initialization and when i add this code windows pop up the message I will create a full topic tomorrow thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.