我得到了这个错误,,"vector" was not declared in this scope '' for the following code when I separate in * h and * cpp a file这是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 );
2条答案
按热度按时间cyvaqqii1#
你已经在cpp文件中得到了声明,在头文件中得到了定义,实际上应该是相反的。
在您交换了文件之后,从functia.h中删除
using namespace std;
,因为在头文件中引入名称空间不是一个好的做法。您需要将声明更改为void fun(std::vector<double> &, double );
参见"using namespace" in c++ headers
我也强烈推荐阅读C/C++ include file order/best practices
62o28rlo2#
将“vector”改为“std::vector”