Say I have something like this

#include <iostream>
#define ICOL 32
using namespace std;

int main(int argc, char *argv[]) {
...

Is there a way that I can change the value of that global variable within main? Simply saying ICOL = 300; does not work...

No. ICOL is not a global variable, it is a #define. A #define is basically just a token (i.e. ICOL) and an expression (i.e. 32). Basically, the preprocessor (a basic parser that looks at your code before it gets compiled) will look through all the code and do a basic find-and-replace operation, finding each instance of ICOL and literally replacing the ICOL by 32 directly in the code, then the code gets compiled. So, doing ICOL = 300; would be the same as doing 32 = 300; which is obviously an error.

To create a global variable, you do this:

#include <iostream>
using namespace std;

int icol = 32;

int main(int argc, char *argv[]) {
  icol = 300; //ok!
 //...

As a note, it is rarely desirable to have a global variable that can be changed (it is bad practice). And make sure your global-variable names are always very unique (they could clash with other variable names in the std namespace for example).

Ok, that makes sense. But now I'm trying to replace these variables and I'm running into trouble with stuff outside of main.

...
using namespace std;

int icol = 32;
int irow = 12;

class tempHolder{
      public: 
      bool C1[icol][irow];
      void operator=(bool C2[icol][irow]){
                for(int i=0;i<irow;i++)
                  for(int ii=0;ii<icol;ii++)
                    C1[i][ii] = C2[i][ii];
      }
};

void printTable(bool CC[][irow], int, bool);

Then I get errors like "data member may not have variably modified type `bool[((unsigned int)((int)icol))][12]'".

The reason I want the global variables is because I have this multidimensional array that gets used throughout the program and I want the user to be able to set the values of the global variables in the command line when executing the program and those variables determine the size of the array. I'm thinking that global variables are the best way to do it, but if anyone has a better suggestion I'd welcome it.

In the meantime, how would I get a global variable to work in prototype declarations or classes like above?

Why don't you declare your variables inside main?

So you want a matrix of bools or ints?

U want dynamic memory allocation? OR U can set a pointer to reference the value of the array. Or a pointer to the array.. whatever your choice.


Lets the user choose the size of the array.
http://cplusplus.com/doc/tutorial/dynamic/

Best of all make the ints members of the class.

Why don't you declare your variables inside main?

Because I need to use the variables outside of main.

So you want a matrix of bools or ints?

It's a matrix of bools, but the size of the array are declared by int's.

If I declare a global variable like this:

const int newRow = 12;

then it works, but then I can't change it since it's a constant.

The error you got is expected. A static array requires a value that is known at compile-time. You cannot let the user choose the size, because that implies that the size would be determined at run-time. The solution: use a dynamic array.

The easiest, in C++, is to use std::vector (#include <vector>). Then, you can declare it as follows:

class tempHolder{
  public: 
    int icol, irow;
    std::vector<bool> C1;
    tempHolder(int numCols, int numRows) : icol(numCols), irow(numRows), C1(numCols * numRows) { /*initialize C1 to whatever you want*/ };
    //no need for a copy-constructor or assignment operator.
    //... the rest of your functions.
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.