如果我#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但仍然不是很好.
| 归档时间: |
|
| 查看次数: |
22105 次 |
| 最近记录: |