gpt4 book ai didi

c++ - 包含 "vector.h"或 "vector"会导致警告或错误

转载 作者:可可西里 更新时间:2023-11-01 15:30:02 24 4
gpt4 key购买 nike

如果我输入 #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

如果我只是添加常规 #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**

有没有更好的方法来包含 vector header ,这样它就不会提示?这是生成警告/错误的源文件:

// 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_*/

最佳答案

问题是 vector<T>住在std命名空间,您正试图在没有任何限定或不适当的情况下使用该类型 using陈述。最安全的解决方法是使用 std 显式限定类型的使用。字首。

std::vector<neuron> nrns;

这也可以通过 using 显式导入类型来解决。陈述。

using std::vector;

不过我会避免这种方法。添加using对头文件的声明虽然合法,但却是不好的做法,因为它可以改变项目的编译方式。这种形式比一揽子导入 std 更安全但仍然不是很好。

关于c++ - 包含 "vector.h"或 "vector"会导致警告或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3874829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com