6

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 );
Mihaela
  • 121
  • 1
  • 1
  • 6

2 Answers2

14

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

Community
  • 1
  • 1
virgesmith
  • 762
  • 1
  • 7
  • 18
0

Change "vector" to "std::vector"

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34371992) – user16217248 May 15 '23 at 23:19