包括"vector.h"或"vector"会导致警告或错误

Ted*_*Ted 10 c++

如果我#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   
Run Code Online (Sandbox Code Playgroud)

如果我只是添加常规#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**
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法来包含矢量标头这样它不会抱怨?这是生成警告/错误的源文件:

// 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_*/
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 30

问题是vector<T>生活在std名称空间中,并且您尝试使用该类型而没有任何限定条件或适当的using语句.最安全的解决方法是明确限定使用带std前缀的类型.

std::vector<neuron> nrns;
Run Code Online (Sandbox Code Playgroud)

这也可以通过using语句显式导入类型来解决.

using std::vector;
Run Code Online (Sandbox Code Playgroud)

我会避免这种方法.using在合法的情况下向头文件添加语句是不好的做法,因为它可以改变项目的编译方式.这种形式比一揽子进口更安全,std但仍然不是很好.

  • "使用命名空间......"被足够多的人不屑于考虑替代方案 - 只用"使用"你真正需要的东西.即`使用std :: vector;`. (9认同)
  • 对不起杰瑞德,但是我真心的`-1`列出了最差的替代品.(很多人不会再读了.) (3认同)
  • @Jared:这不是个人喜好.限定`std :: vector`,`使用std :: vector`和`using namespace std;`do*different*things,最后两个被认为是标题中的错误做法; 并经常在源文件中.个人偏好是支撑式,而不是完全不同的行为.如果有人问"我如何得到一个动态数组"并且回答说"首先使用`new int [size]`"或者没有提到`std :: vector <int>`它也会得到一个downvote,因为教人们除了惯用的C++之外的东西也没有帮助.同样在这里.不要在标头中使用声明. (2认同)

Kir*_*sky 15

vector属于std命名空间.您需要完全限定其名称std::vector<int>.

我需要澄清一下,C++标准允许你使用JaredPar在他的答案中给出的所有选项,但我强烈建议不要使用using namespace std,特别是在头文件中.关于using namespace std你可以在这个问题中找到很好的描述.我个人同意,所以请允许我在答案中将其链接起来.