11

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_*/
outis
  • 75,655
  • 22
  • 151
  • 221
Ted
  • 111
  • 1
  • 1
  • 3
  • 3
    if you are writing c++ code, don't mess up with c style headers. use & . the c style headers can be used as c++ style header: . – Donotalo Oct 06 '10 at 16:53
  • 6
    `_MYNN_H_` is a reserved identifier, [don't use it](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – GManNickG Oct 06 '10 at 18:28

3 Answers3

31

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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    You mean `using namespace std;` – Fred Larson Oct 06 '10 at 16:40
  • 9
    `using namespace ...` is frowned up by enough people to consider the alternative - only `using` what you really need. I.e. `using std::vector;`. –  Oct 06 '10 at 16:42
  • 3
    Sorry Jared, but a heartfelt `-1` from me for listing the worst alternative first. (Many won't read any further.) – sbi Oct 06 '10 at 17:23
  • @sbi, it's your decision but it's a matter of personal preference and opinion that you're downvoting me on. The answer is correct (all 3 of them I provided). If you feel you have a better answer then add it and your reasons. Downvoting seems inappropriate. – JaredPar Oct 06 '10 at 17:32
  • @Jared: You just ran into [one of my pet peeves](http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136). Sorry for that, but it's not as if your answer would need my vote. (Usually a public down-vote seen as unjustified is more than made up for by the community.) Also, Kirill has already given the answer I would have (which is why I up-voted his). – sbi Oct 06 '10 at 17:47
  • Because when I include `mynn.h` I want the entire `std` namespace thrown into the global one...sorry, I'm with @sbi on this. Obviously this is all subjective in an ivory tower, but in practice you've listed the worse solution first. – GManNickG Oct 06 '10 at 18:30
  • @GMan, @sbi, fine, changed my answer order. I still think it's bad practice to downvote a valid answer on a matter of personal preference. All my answers are valid. If you think there is a better answer then provide it vs. downvoting a correct one. – JaredPar Oct 06 '10 at 18:38
  • 2
    @Jared: It's not a personal preference. Qualifying `std::vector`, `using std::vector` and `using namespace std;` do *different* things, and the last two are regarded as bad practice in a header; and often times in a source file. Personal preference is brace style, not completely different behaviors. If someone asked "how do I get a dynamic array" and an answer said "use `new int[size]`" either first or without mentioning `std::vector` it would also get a downvote, because teaching people something besides idiomatic C++ isn't helpful. Same here. Don't put using declarations in a header. – GManNickG Oct 06 '10 at 18:57
  • 1
    @GMan, My bad. I read this as being a source file (not a header) file. Agree it is evil in a header file. – JaredPar Oct 06 '10 at 19:07
  • @Jared: It's okay. I'll still say I disagree with `using std::vector`, but you've removed the bad part so -1 goes away. (Also, the answer feels a bit weird with "The problem is that you haven't imported the std namespace", since that's the bad part and not what your answer does.) – GManNickG Oct 06 '10 at 19:28
  • Jared, I hadn't seen your reply (why, oh why does SO not show replies to several commentators?), but your Twitter rant brought me back here. `:)` Turned my down-vote into an up-vote. – sbi Oct 08 '10 at 16:10
  • @sbi, I think it was my fault for not putting your alias in the comment response. I believe it just looks at the names listed directly in every comment. But yeah, total misread of the question by me. – JaredPar Oct 10 '10 at 06:07
  • @Jared: You did list my name, you even properly @attributed it, but GMan's name was first, and SO only notifies the first commenter you @address. (FWIW, I would have down-voted even if it wasn't a header. SO you're still entitled to your rant. `:)`) – sbi Oct 10 '10 at 06:52
14

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.

Community
  • 1
  • 1
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
2

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.

dzada
  • 5,344
  • 5
  • 29
  • 37