C++ “向量”未在此范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42808431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:31:20  来源:igfitidea点击:

"vector" was not declared in this scope

c++vector

提问by Mihaela

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:

我收到这个错误,当我在 *h 和 *cpp 中分离一个文件时,以下代码未在此范围内声明“向量”这是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:

功能.h:

 void fun(vector<double> & rS, double a)
 {
   rS[0] = a + 3;
   rS[1] = 4;
   rS[2] = 5;
 }

functia.cpp:

功能.cpp:

#include <iostream>
#include <math.h>
#include<vector>

using namespace std;


void fun(vector<double> &, double );

回答by virgesmith

You've got the declaration in the cpp file and the definition in the header, it should really be the other way round.

你已经在 cpp 文件中得到了声明,在头文件中得到了定义,它应该是相反的。

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

在您交换文件后,using namespace std;从 functia.h 中删除,因为在头文件中引入命名空间并不是一个好习惯。您需要将声明更改为

void fun(std::vector<double> &, double );

void fun(std::vector<double> &, double );

See "using namespace" in c++ headers

请参阅C++ 头文件中的“使用命名空间”

I'd also strongly recommend reading C/C++ include file order/best practices

我还强烈建议阅读C/C++ 包括文件顺序/最佳实践