cpp error if trying to declare an array in another file - c++

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

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.

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.

Related

How to code a library with a user defined parameter in C/C++?

Is it possible to code a library with a parameter that will be defined by the user at compile time without modifying the library?
Library:
test.h
#ifndef TEST_H
#define TEST_H
#define MAX_NB USER_DEF
void myFunc();
#endif
test.cpp
#include "test.h"
int arr[MAX_NB] = {0};
void myFunc() {
for (int i = 0: i < MAX_NB; i++ ) {
// DO SOMETHING
}
}
And the main code:
#define USER_DEF 5
#include "test.h"
void main() {
while (1) {
myFunc();
}
}
I suppose I am getting an error (USER_DEF not declared in this scope) because USER_DEF is not defined in the translation unit containing test.h and test.c, but I hope there is a way to do it, but I can't find it.
My goal is to have an array with a user-defined size (code modified).
The answer is that it doesn't make sense to use variable-sized arrays for a very restricted 8-bit system with very limited RAM.
Instead you should have a fixed size array of n bytes. Let the user pick a number, then ensure that it is less or equal to n. Then keep track of the used size with a plain integer variable. You must always reserve memory for the worst case.
The array must have static storage duration, since it will be too large to be allocated on the stack.
I'd usually go and suggest to check out the templates, although, you can create user-defined variables, they are called function parameters. Please take a look
To sum it up, this is not what you call user-defined variable, because this is defined by the programmer, not by the user of the program. This is called a constant, whatsoever. If you want to create a compile-time constant, defined by you, you can take a look at constexpr.
Also, let me explain to you why this doesn't work correctly. You guessed correctly, it's because it is not defined into the header file, but why? Because headers come first and then comes the main file. The header searches for the defined constant and fails to find it, and that is the reason why you get the USER_DEF not declared in this scope.

C++ unknown name type

I have a header file defining some parameters. I have defined some of the parameters as extern. My program works fine with other data types such as double and int, except when I try to add vector variables. The declaration in header file is
extern std::vector<double> my_vec;
In my main file, I am constructing the vector using this code:
std::vector<double> my_vec(3,0);
When I try to clear the vector using the clear method, the compiler is giving an error saying that unknown type. I am not even sure how to debug this. Can someone help?
P.S. I was originally trying to assign some values to this vector using:
my_vec[0] = 1;
but the compiler says that C++ requires a type specifier for all declarations. I googled this error, but I don't understand because I am specifying the type of my_vec.
Edit: example:
main.cpp
#include "params.h"
#include <vector>
std::vector<double> my_vec(3,0);
my_vec.clear();
// edit: my_vec[0] = 1; this also produces an error
int main(){
return 0;
}
params.h
#include <vector>
extern std::vector<double> my_vec;
Error message:
main.cpp:6:1: error: unknown type name 'my_vec'
my_vec.clear();
^
main.cpp:6:7: error: cannot use dot operator on a type
my_vec.clear();
^
2 errors generated.
You can't execute statements outside of a function - which is what you're trying to do with my_vec.clear();. It doesn't matter that clear() is a method of the vector class - invoking a method (as opposed to constructing a variable) is a statement, just like x = 1; . Those belong in functions.
You have to put your statement somewhere in your main(), e.g.:
int main(){
my_vec.clear();
return 0;
}
or make sure and construct my_vec the way you want it to look like, to begin with.
Also, more generally, you should avoid global variables if you don't really need them. And - you very rarely do. See:
Are global variables bad?
Edit: OP asks whether we can get around this restriction somehow. First - you really shouldn't (see what I just said). But it is possible: We can use a static block, which is implementable in C++, sort of.

C ++: Cannot I assign values to a variable in header file?

header.h
int m_linkinfo;
m_linkinfo = 1;
main.cpp
#include "header.h"
int main()
{
return 0;
}
Failed, with many errors. But if I commented the line m_linkinfo = 1;, everything is OK. Why? Cannot I assign values to a variable in header file? But If I changed the header file to the only one line: int m_linkinfo = 1;, The program is compiled successfully! Why? Is it different from the two lines of code above?
No, you can't. That's a piece of code so it needs to exist inside a function of some sort, such as:
int main () {
m_linkinfo = 1;
return 0;
}
You can, as you have seen, initialise it with:
int m_linkinfo = 1;
however, since that's allowed by the standard.
Keep in mind that it's often risky to define things in header files. By define, I mean statements that create things as opposed to those that simply notify the compiler than things exist (declaring).
That's because including the header in two different translation units can result in two copies of a thing with the same name and, if you subsequently try to link them together, you'll run into trouble.
The best way to solve that is to declare things in header files, such as:
extern int m_linkinfo;
and define them in a non-header (eg, CPP) file:
int m_linkinfo = 1;
That way, every translation unit that includes the header knows about m_linkinfo but only the CPP file creates it.
C doesn't allow code outside of functions. In your example:
int m_linkinfo;
m_linkinfo = 1;
The second line is illegal, since it isn't in a function.
Outside of functions you can only declare or define variables and functions (or give directions to the preprocessor).
However, you are allowed to initalise a variable when you define it, so you can do this:
int m_linkinfo = 1;
which is perfectly legal.
Assignment is a statement. Statements are only allowed in functions. The line in the header file is not in a function. Therefore it cannot work.

C++ Giving a global variable an initial value

I'm writing code in Visual C++ and I need to use a global variable. I know it's generally not a good idea, but in this case, it is necessary. I have created the variable and it is accessible from the function that needs it, but I can't figure out how to give it an initial value.
in the .h file it looks like
extern int lversion;
and in the .cpp file it looks like
int lversion;
How can I give this variable an initial value of 0?
Adding these two lines in your .h and .cpp files respectively will allow you to initialize a global variable.
.h:
extern int Val;
.cpp:
int Val = 0;
The variable does have initial value 0 as it is. Globals and statics are value-initialized unless otherwise specified. (for an int, it will be 0)
If you want any other value, you can specify it:
In the cpp file:
int lversion= 3;
or
int lversion(3);
but for a value of 0 there's no point being this verbose.

C++ "dynamic" arrays

I've got some problems/misunderstandings with arrays in C++.
int myArray[30];
myArray[1]=2;
myArray[2]=4;
This is spitting out a lot of compiler errors. I don't think it is necessary to include them here as this is an easy question for everybody with experience in C(++) I guess
Why doesn't this work?
Is there a way to create a "dynamic" array that has a fixed ammount of values (so no malloc needed) but where I can change the values during runtime?
I'm guessing you have that outside of a function.
You are allowed to define variables outside of a function. You can even call arbitrary code outside of a function provided it is part of a variable definition.
// legal outside of a function
int myArray[30];
int x = arbitrary_code();
void foo()
{
}
But you cannot have arbitrary statements or expressions outside of a function.
// ILLEGAL outside a function
myArray[1] = 5;
void foo()
{
// But legal inside a function
myArray[2] = 10;
}
Are you saying that this doesn't compile:
int main() {
int myArray[30];
myArray[1]=2;
myArray[2]=4;
}
If it doesn't, you have something wrong with your compiler setup. As I said in my comment, we need to see the error messages.