0

In code below I can't push the initial table in the deque and I can't assign the front of the deque in a value x.

Can someone tell me how to fix this?

What should I use instead of a char array in order to make this program work?

#include <iostream>
#include <deque>
using namespace std;

char x[4][4];
deque<char [4][4]> myStack;

char func(char initial[4][4])
{
     myStack.push_front(initial[4][4]); // ERROR1
     x[4][4]=myStack.front();            // ERROR2
}

Attempting to compile the above produces the errors:

ERROR1: no instance of overloaded function "std::deque<_Ty, _Ax>::push_front [with _Ty=char [4][4], _Ax=std::allocator<char [4][4]>]" matches the argument list

ERROR2: a value of type "char (*)[4]" cannot be assigned to an entity of type "char"

george mano
  • 5,948
  • 6
  • 33
  • 43

3 Answers3

1

You cannot store C-style arrays in containers.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

C style arrays are not first class citizens in C or C++. They cannot be copied, so you cannot put the array in a container.

One possible solution is to store the array inside a struct, and then store the struct in the deque. Another idea can be to use std::string instead of char arrays.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • I use the char array because I need to handle a 15-puzzle. Do you know any way to make a stack with [4][4] arrays inside? – george mano Jan 21 '12 at 17:07
  • 1
    You can have a `struct puzzle` containing the array. The `struct` can then be stored in a container (like deque). – Bo Persson Jan 21 '12 at 17:12
1

There are a handful of issues here with the way you're trying to use arrays.

  1. The arrays in your program are not called initial[4][4] or x[4][4]. They are called initial and x.
  2. Arrays cannot be passed by value into a function.
  3. Arrays cannot be stored in standard containers.

As an initial fix, you could use the type boost::array<boost::array<char, 4>, 4> instead; since it's an object wrapper around an array, this will work. However it strikes me that storing a multi-dimensional array in a container is probably wrong; depending on your requirements, you are looking to do something else. Unfortunately, we don't know what your requirements are, so I cannot suggest specific alternatives.

Also, calling a deque myStack is very confusing. Stacks are something else.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Yes... and `initial[4][4]` and `x[4][4]` are names for elements outside the bounds of those arrays. – Ben Voigt Jan 21 '12 at 16:56
  • @Lightness I am using deque to implement a stack. Is this wrong ? – george mano Jan 21 '12 at 17:09
  • @georgemano: You're certainly _doing it_ wrong. And, yes, I'd say so. What's wrong with `std::stack`? – Lightness Races in Orbit Jan 21 '12 at 17:11
  • @LightnessRacesinOrbit I switched from stack to deque because of this http://stackoverflow.com/questions/8918762/find-element-in-stack . – george mano Jan 21 '12 at 17:29
  • @LightnessRacesinOrbit Way did you say that in my program the arrays are called `x` but they are not called `x[4][4]`? Where should I change my code? – george mano Jan 21 '12 at 17:32
  • @george: Nothing in that question tells you to "implement a stack in deque". Just use a deque, normally. You had a `stack` before, so why are you not using `deque` now? Why the arrays? – Lightness Races in Orbit Jan 21 '12 at 17:43
  • @LightnessRacesinOrbit Because I am using [4][4] arrays. Because I am trying to solve a problem like [this](http://www.javaonthebrain.com/java/puzz15/). Is the Deque structure wrong in order to use it as a stack of 4x4 arrays ? – george mano Jan 21 '12 at 17:52
  • @georgemano: The answer to "why arrays" is not "because arrays". I'm asking because you were not using arrays in your previous attempt. I have no idea why you think you need a deque of arrays of arrays for solving that puzzle. You haven't explained that at all. – Lightness Races in Orbit Jan 21 '12 at 17:56
  • @LightnessRacesinOrbit 15-puzzle is the problem I am trying to solve. So, I have to use arrays, I believe. **Do you have another opinion?** In my previous posts I wasnt using arrays because I wanted to make the algorithm(dfs) with a character and after to transform the char to char-array. Now I believe this thought was wrong. – george mano Jan 21 '12 at 18:04
  • @georgemano: Repeating over and over that you're working on a "15-puzzle" is not helpful, and **it's not what I asked you**. I asked _why do you think you need arrays_? – Lightness Races in Orbit Jan 21 '12 at 18:37
  • @georgemano: Alright, I've worked it out on my own: your container represents _states_ in the game, and each state is represented by one `char` per board position. Why didn't you just say that? – Lightness Races in Orbit Jan 21 '12 at 18:45
  • @LightnessRacesinOrbit You are right, I should have explained more the problem. What do you suggest me to do with the problem I have. What type of `array` should I use? What type of `stack` should I use? Thanks for the help in advance. – george mano Jan 21 '12 at 23:46