2

I am creating a class that has a 2D array as a variable. I want users of the class to be able to use it with both a normal 2D array created on the stack ( int array[3][3] ) or be able to use it with a dynamic 2D array on the heap ( int *array[3] ). Then I will fill my class's 2D array with the incoming array no matter which type it is.

I have looked at this post: Passing a 2D array to a C++ function and a few others very similar. I followed the answers but I am still having issues. Here is what I am doing:

My class is called CurrentState, and I have two constructors with different signatures.

CurrentState.h

#ifndef A_STAR_CURRENTSTATE_H
#define A_STAR_CURRENTSTATE_H

class CurrentState
{
    private:
        int state[3][3];

    public:
        CurrentState(int** state);      // Dynamic array 
        CurrentState(int state[][3]);  // Normal array
        bool isFinishedState;
        bool checkFinishedState();
};

#endif 

CurrentState.cpp

#include <iostream>
#include "currentState.h"

CurrentState::CurrentState(int** state) {

    // Fill class's state array with incoming array
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            this->state[i][j] = state[i][j];
    }
}

CurrentState::CurrentState(int state[][3])
{        
    // Fill class's state array with incoming array
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            this->state[i][j] = state[i][j];
    }
}

Then in my main() method I am doing this:

#include <iostream>
#include "currentState.h"

using namespace std;



int main()
{
    // Make dynamic array and pass into constructor
    int *array[3];
    for (int i = 0; i < 3; i++)
        array[i] = new int[3];

    CurrentState dynamic(array);

    // Make normal array and pass into constructor
    int a[3][3];

    CurrentState onStack(a); // <-- error says: "Class 'CurrentState' does not have a constructor 'CurrentState(int [3][3])'"


    return 0;
}

The first initiation of CurrentState in main() with the dynamic array works fine, however the second initiation of CurrentState with the normal array gives an error.

I am using CLion IDE by JetBrains, the method is underlined red and says: "Class 'CurrentState' does not have a constructor 'CurrentState(int[3][3])'"

Am I missing something? I'm pretty sure the class does have a constructor for a normal array ( int[3][3] ).

I have seen numerous other people in my searching doing the same thing, like here: http://www.cplusplus.com/forum/beginner/73432/ and in the link I posted at the beginning of my post.

Am I overlooking something? Any help will be greatly appreciated.

EDIT

I have removed the first parameter from the array and made it like so:

CurrentState(int state[][3]);

And I still have the same error

EDIt

From the command line it does compile, however it does not compile from within the IDE. I will just ignore the error while coding. False Alarm. Sorry people.

Community
  • 1
  • 1
BradStell
  • 331
  • 2
  • 12

1 Answers1

2

Remove the first value from the bracket in your function definition.

Like so:

CurrentState::CurrentState(int state[][3])
{        
    ...
}

I'd also strongly recommend using std::array or std::vector as opposed to the c-style array. Your code for dynamic allocation already leaks memory unless you add the release loop below.

int *array[3];
for (int i = 0; i < 3; i++)
    array[i] = new int[3];

CurrentState dynamic(array);

//Kind of ugly cleanup here
for (int i = 0; i < 3; i++)
    delete[] array[i];
davepmiller
  • 2,200
  • 3
  • 27
  • 52
  • His code compiles, see [here](http://ideone.com/LCSFg3). The first value is discarded by the compiler anyway, so it doesn't matter what number you put there. – Allanqunzi Sep 04 '15 at 20:58
  • What compiler are you using through CLion? This works fine with g++ 4.8.4. – davepmiller Sep 04 '15 at 21:05
  • A quick explanation of why this is required would strengthen this answer. And since the array has known and fixed size and not dynamically allocated, there is little additional benefit to using a vector. – user4581301 Sep 04 '15 at 21:06
  • I am using MinGW as my c++ compiler. From the command line it does compile, however Clion (using the same compiler) it does not compile. Not a big deal I guess I will just compile from the command line. – BradStell Sep 04 '15 at 21:07
  • 1
    @BradStell Probably worth looking at the command line options used by CLion to see if there are any differences with what you used – user4581301 Sep 04 '15 at 21:09
  • @user4581301 Part of the reason I suggested std containers is that we're already leaking memory here. And the cleanup is not super intuitive. – davepmiller Sep 04 '15 at 21:15
  • Not super intuitive indeed. Only leak I see is the dynamic array construction down in main. The class should be fine. Am I missing something? – user4581301 Sep 04 '15 at 21:19
  • What if I delete the dynamic array, in the constructor, after I'm done copying its contents over? – BradStell Sep 04 '15 at 21:19
  • @BradStell that works here, but in a non-trivial program with the threat of exceptions and non-linear code flow, the automated destruction gained from std::array and std::vector will save a lot of headaches. – user4581301 Sep 04 '15 at 21:22
  • Yes Brad I added that code to the answer. @user4581301 You're not missing anything. :) Just that the C++ spec recommends using std::vector for dynamic array-style containers so these situations are avoided. – davepmiller Sep 04 '15 at 21:23
  • I can add a constructor to work with vectors, and I will use them personally. But I do not know if the person running this code (ie. my teacher) will want to pass it a dynamic array, this is why I am including this constructor. – BradStell Sep 04 '15 at 21:25
  • @BradStell that is unfortunate. The assignment spec should make the interface clear.Otherwise you don't know if have to make CurrentState(int one, int two, int three, ..., int nine) and other stupid combinations Laser_Wizard I think we're in agreement on this, just looking at it from different sides. Dynamic array bad. Vector good, but statically sized array also good. – user4581301 Sep 05 '15 at 01:14