-2

I am attempting to create a two dimensional array depending on the users desired number of rows and columns. I am receiving an error of "use of undeclared identifier 'rows'". I googled and searched on stack overflow but was not able to find a scenario like this, I would like to know what am I doing wrong below is my code :

    #include <iostream>
using namespace std;

class Matrix{
    public:
    int matrixDimensions[rows][columns];

    void setMatrix(int x, int y){
        rows = x;
        columns = y;
   }
    int getMatrixDimensions(){
        return rows;
        return columns;

    }



    private:
    int rows;
    int columns;

};

int main(int argc, const char * argv[]) {
    int a;
    int b;
    Matrix matrixObject;

    cout << "Please enter the number of rows: " << endl;
    cin >> a;
    cout << "Please enter the number of columns: "<< endl;
    cin >> b;

    matrixObject.setMatrix(a, b);
    cout << "The number of rows and columns are : " << matrixObject.getMatrixDimensions();



    return 0;
}

Thank you all feedback is welcomed.I cannot use vectors, thank you for mentioning them but it is not an option here.

Ace
  • 603
  • 2
  • 15
  • 33
  • `int matrixDimensions[rows][columns];` is illegal in c++ unless `rows` and `columns` are compile time constants. – drescherjm Jan 30 '18 at 02:20
  • Do you know about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)? If not then it's time you learn. Use a vector of vectors. – Some programmer dude Jan 30 '18 at 02:21
  • Think like a compiler. You see the line `int matrixDimensions[rows][columns];` and you know you have to allocate some memory. How much? That depends on `rows` and `columns` which are symbols you haven't seen yet. Even if you had seen them they don't have any values assigned until after you call `setMatrix()`. Also: Your `getMatrixDimensions()` tells me you need to go back to your text book/ online course / tutor to brush up on some basics. – John3136 Jan 30 '18 at 02:23
  • 1
    Would second the recommendation to use std::vector unless this is an exercise and that isn't allowed, in which case the question should have stated that. – paisanco Jan 30 '18 at 02:24
  • 1
    Possible duplicate of [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Davis Herring Jan 30 '18 at 02:25
  • 1
    @paisanco *I googled and searched on stack overflow but was not able to find a scenario like this,* -- That sounds like someone looking for any solution, not really a school assignment. – PaulMcKenzie Jan 30 '18 at 02:26
  • @PaulMcKenzie I am not looking to have this solved, re-read please I asked for what I am doing wrong not a fix. – Ace Jan 30 '18 at 02:27
  • Ugh why are there always constraints like "I cannot use vector" or string or anything from the standard library? What's the point in learning C++ – Tas Jan 30 '18 at 02:28
  • 3
    @Ace *I cannot use vectors* -- You can't use standard arrays, since they're fixed in size at compile-time. So your attempt will get you nowhere. Time to learn pointers and `new[]` if you can't use vectors. – PaulMcKenzie Jan 30 '18 at 02:28
  • I am just trying to learn and this is part of my course but thank you everyone I believe I know how to fix this mostly thank you @John3136 you clarified my error – Ace Jan 30 '18 at 02:28
  • 1
    @Ace [Look at this answer](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048). That closely as possible mimics a 2-dimensional dynamic array without using vectors, even up to its contiguous nature. – PaulMcKenzie Jan 30 '18 at 02:35

2 Answers2

-1

In C++, if you want to declare an array regardless of dimension, you can only declare it before the compile time.

For example, let SIZE be the user input during run time

int arr[5]    // OK
int arr[SIZE] // NOT OK

If you want to dynamically allocated the array size during the run time, you have to use the pointer.

int* arr = new int [5] // OK
int* arr = new int [SIZE] // OK

Take a look in How do I declare a 2d array in C++ using new?

Edwardhk
  • 87
  • 3
  • 9
  • This doesn't really translate well into the code OP posted, as this code is unrelated. How would you recommend they declare `matrixDimensions` – Tas Jan 30 '18 at 02:30
-1

Try declaring the variables rows and columns before the functions. C++ will check the syntax line by line. So it might be the issue.