0

I wanted to make my code more clear - this is why I made an extra cpp file where I declared an array which is taking a lot of space.

But whenever I try to compile my code it says

error c2466: Assignment of an array of constant size can not be

(I translated from German, so don't wonder if you don't know this error 1by1)

The code in main.cpp (To include the file)

#include "mapOne.cpp"

And the code in mapOne.cpp:

int point[100][100][2];
point [1][0][0] = 1; [...]

Can someone help me? I hate it, if a file is >400 lines long just because there is one array declared...

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    Declare it in .h file and also use extern: http://stackoverflow.com/questions/15841495/the-usage-of-extern-in-c – uchar Apr 29 '14 at 16:03
  • is the initialization code in a function? – Bgie Apr 29 '14 at 16:03
  • If I use a header file there is another error.. (Even if I add extern) error: 'point' does not name a type It is not in a function btw. –  Apr 29 '14 at 16:15
  • If you're putting it into a `.cpp` file, it implies that you're going to compile it as a single unit of compilation to which you can link other object files that will use it. Don't include it in another `.cpp` file as it'll get very confusing and could be declared twice, which is not what you want. – Component 10 Apr 29 '14 at 16:24

2 Answers2

0

You're trying to assign values to your array outside of a function, which isn't allowed. Instead, the compiler assumes you're trying to declare a new array.

Try wrapping the assignments in a function, and call that function before you start using the array.

Daan
  • 484
  • 4
  • 13
-1

The problem you have is happening because you didn't declare the array in an area where your function will be able to use it from. For instance, If I do the following code:

In file1.cpp

int array[20];

In file2.cpp

#include "file1.cpp"

int function1()
{
  i = 1;
  for (int x = 0; x<20; x++)
  {
    array[x] = i;
    i = i + 2;
  }
}

The array[x] would not be recognized. The reason it is not recognized is because even if you use the "include" code on top, you are only including the ability to use the functions that are present in the file1.cpp file. The reason you are only allowed to use the functions and not the variables is simply because the compilers don't want to mix the variables you declare in file1.cpp and file2.cpp. This makes sense because a lot of times you'll declare variables of the same name in different files because it is simpler.

What you can do however is declare the array in a header file. If your writing your function in file2.cpp, you create a header file called file2.h:

In file2.h:

class file2
{
    public:
    int array[20];///or whichever type of array you want to declare

}

It is important you keep the variables under the "public:" section so that all the functions you make in the file2.cpp can use it.

computerWizard
  • 94
  • 3
  • 12
  • The advice you've given will fix the problem, and is arguably better style than including a .cpp file. But the reasoning behind it is wrong. Variables declared in global scope *will* be included. So your initial example compiles and runs fine (as long as you include a type for `i`) – Daan Apr 29 '14 at 20:30
  • I disagree the reasoning behind it is wrong. I know variables declared in global scope will be included, but that is "another option". The fact that there are other options doesn't make "my option" have a wrong reasoning. Everything I mentioned is in fact correct and in most cases the organized and preferred method of choice. – computerWizard Apr 29 '14 at 20:52
  • I meant the part were you said "The array[x] would not be recognized." and "you are only including the ability to use the functions". Your wording suggests that this is what the compiler does. But it doesn't, which is actually one of the reasons we tend to prefer doing things the way the rest of your answer suggests. – Daan Apr 29 '14 at 20:58