Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"vector" was not declared in this scope

Tags:

c++

vector

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 );
like image 872
Mihaela Avatar asked Mar 15 '17 11:03

Mihaela


People also ask

How do I fix this function was not declared in this scope?

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.

What does it mean if was not declared in this scope?

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

How do you declare a vector?

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.

What happens when a vector goes out of scope?

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


1 Answers

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

like image 55
virgesmith Avatar answered Sep 30 '22 16:09

virgesmith