"vector" was not declared in this scope
Asked Answered
B

2

7

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 );
Bushey answered 15/3, 2017 at 11:26 Comment(6)
Why does you .cpp contain the declaration and you .h file the definition of fun(...)? Something seems odd there.Imitative
i already tried, it is not workingBushey
This is how I learned: in .cpp the declaration and in .h the definition. It is working in that way for files without ,,vector''.Bushey
Lots of bugs in code, its not just vector error, solve other bugs vector won't be issue.Germann
@MIhaela, As soon as two files include the same header, you violate the One Definition Rule.Pharmacist
"This is how I learned: in .cpp the declaration and in .h the definition" That is backwards.Glutenous
E
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

Edvard answered 15/3, 2017 at 11:38 Comment(0)
N
0

Change "vector" to "std::vector"

Neoplasty answered 11/5, 2023 at 14:59 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewOrthodontia

© 2022 - 2024 — McMap. All rights reserved.