I got this error ,,"vector" was not declared in this scope'' for the following code when I separate in *h and *cpp a file This is the main.cpp:
#include <iostream>
#include <math.h>
#include <vector>
#include "functia.h"
using namespace std;
int main()
{
vector<double> s(3);
double b= 4;
fun(s, b);
cout<<s[0]<<endl;
double c= 9;
fun(s, c);
cout<<s[0];
}
functia.h:
void fun(vector<double> & rS, double a)
{
rS[0] = a + 3;
rS[1] = 4;
rS[2] = 5;
}
functia.cpp:
#include <iostream>
#include <math.h>
#include<vector>
using namespace std;
void fun(vector<double> &, double );
To resolve this error, a first method that is helpful would be declaring the function prototype before the main() method. So, we have used the function prototype before the main method in the updated code. When we have compiled the code, it throws no exceptions and runs properly.
As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the ...
Syntax. Keyword “vector”: The keyword “vector” is provided at the beginning to declare a vector in C++. type: This parameter is the data type of the elements that are going to be stored in the vector. vector_name: This is the user-specified variable name of the vector.
The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope. This means that the object cannot be accessed in a nesting scope (block).
You've got the declaration in the cpp file and the definition in the header, it should really be the other way round.
After you've swapped the files round remove using namespace std;
from functia.h as it's not good practice to pull in namespaces in header files. You'll need to change the declaration to
void fun(std::vector<double> &, double );
See "using namespace" in c++ headers
I'd also strongly recommend reading C/C++ include file order/best practices
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With