Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including "vector.h" or "vector" causes warnings or errors

Tags:

c++

If I put #include <vector.h> in my source file, I get this warning:

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:

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_*/
like image 571
Ted Avatar asked Oct 06 '10 16:10

Ted


3 Answers

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.

std::vector<neuron> nrns;

This can also be fixed by explicitly importing the type via a using statement.

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.

like image 104
JaredPar Avatar answered Nov 11 '22 16:11

JaredPar


vector belongs to std namespace. You need to fully qualify its name as 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 this question. Personally I'm agree with it, so allow me to link it in my answer.

like image 34
Kirill V. Lyadvinsky Avatar answered Nov 11 '22 15:11

Kirill V. Lyadvinsky


Indeed, you need to specify std::vector as vector is not global. But I would rather advice you to NOT use using keyword.

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.

like image 40
dzada Avatar answered Nov 11 '22 16:11

dzada