C++ 包含“vector.h”或“vector”会导致警告或错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3874829/
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
Including "vector.h" or "vector" causes warnings or errors
提问by Ted
If I put #include <vector.h>
in my source file, I get this warning:
如果我放入#include <vector.h>
源文件,则会收到此警告:
make -f Makefile CFG=Debug
g++ -c -g -o "Debug/mynn.o" "mynn.cpp"
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/vector.h:59,
from mynn.h:7,
from mynn.cpp:1:
**C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.**
g++ -g -o "Debug/mynn.exe" Debug/mynn.o
and if I just add regular #include <vector>
(without .h, like the warning suggests), I get the following errors:
如果我只是添加常规#include <vector>
(没有 .h,就像警告所暗示的那样),我会收到以下错误:
make -f Makefile CFG=Debug
g++ -c -g -o "Debug/mynn.o" "mynn.cpp"
In file included from mynn.cpp:1:
**mynn.h:12: error: ISO C++ forbids declaration of `vector' with no type
mynn.h:12: error: expected `;' before '<' token
mynn.h:13: error: `vector' has not been declared
mynn.h:13: error: expected `,' or `...' before '<' token
mynn.h:13: error: ISO C++ forbids declaration of `parameter' with no type
mynn.h:20: error: ISO C++ forbids declaration of `vector' with no type
mynn.h:20: error: expected `;' before '<' token
mynn.h:21: error: ISO C++ forbids declaration of `vector' with no type
mynn.h:21: error: expected `;' before '<' token**
Is there a better way to include the vector header this so that it doesn't complain? Here's the source file that generates the warnings/errors:
有没有更好的方法来包含矢量标头,这样它就不会抱怨?这是生成警告/错误的源文件:
// mynn.h
#ifndef _MYNN_H_
#define _MYNN_H_
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
class neuron {
public:
neuron();
vector<int> weights;
int compute_sum (vector <int> &input);
};
class layer
{
public:
layer();
vector <neuron> nrns;
vector<int> compute_layer (vector <int> &input);
};
#endif /*_MYNN_H_*/
回答by JaredPar
The problem is that vector<T>
lives in the std
namespace and you're attempting to use the type without any qualification or appropriate using
statement. The safest fix is to explicitly qualify uses of the type with the std
prefix.
问题在于它vector<T>
存在于std
命名空间中,而您试图在没有任何限定或适当using
语句的情况下使用该类型。最安全的解决方法是使用std
前缀明确限定类型的使用。
std::vector<neuron> nrns;
This can also be fixed by explicitly importing the type via a using
statement.
这也可以通过通过using
语句显式导入类型来解决。
using std::vector;
I would avoid this approach though. Adding using
statements to header files, while legal, is bad practice because it can change how items are compiled. This form safer than a blanket import of std
but is still not great.
不过我会避免这种方法。向using
头文件添加语句虽然合法,但不是很好的做法,因为它会改变项目的编译方式。这种形式比一揽子导入更安全,std
但仍然不是很好。
回答by Kirill V. Lyadvinsky
vector
belongs to std
namespace. You need to fully qualify its name as std::vector<int>
.
vector
属于std
命名空间。您需要将其名称完全限定为std::vector<int>
.
I need to clarify that the C++ Standard allows you to use all options that JaredPar gave in his answer, but I would strongly recommend not to use using namespace std
and especially in the header files. About using namespace std
you can find well described opinion in thisquestion. Personally I'm agree with it, so allow me to link it in my answer.
我需要澄清的是,C++ 标准允许您使用 JaredPar 在他的回答中给出的所有选项,但我强烈建议不要使用using namespace std
,尤其是在头文件中。关于using namespace std
您可以在此问题中找到详细描述的意见。我个人同意它,所以请允许我将其链接到我的答案中。
回答by dzada
Indeed,
you need to specify std::vector as vector is not global.
But I would rather advice you to NOT use using
keyword.
实际上,您需要指定 std::vector 因为 vector 不是全局的。但我宁愿建议您不要使用using
关键字。
The problem is the scope of the using, and the conflicts that could raise after. MOREOVER if you're planning to have a portable apps (code), (especially for library) you should avoid sush a thing because you can't be sure of the side effects on other plateforms, for the future users of your code.
问题是使用的范围,以及之后可能引发的冲突。此外,如果您打算拥有一个可移植的应用程序(代码),(尤其是对于库),您应该避免 sush 一个东西,因为您无法确定对其他平台的副作用,对于您的代码的未来用户。