質問

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...

役に立ちましたか?

解決 2

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top