Reputation: 500
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.
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
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.
Upvotes: 2
Views: 114
Reputation: 2708
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];
Upvotes: 2