tags:

views:

254

answers:

3

if I put: #include vector.h

I get this annoying 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 it suggests)

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 do this so that it doesn't complain:

// 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_*/
+21  A: 

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
You mean `using namespace std;`
Fred Larson
`using namespace ...` is frowned up by enough people to consider the alternative - only `using` what you really need. I.e. `using std::vector;`.
delnan
@Fred, doh thanks
JaredPar
This used to get me when I was learning C++.
Ed Swangren
Sorry Jared, but a heartfelt `-1` from me for listing the worst alternative first. (Many won't read any further.)
sbi
@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
@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
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.
GMan
@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
@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<int>` 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.
GMan
@GMan, My bad. I read this as being a source file (not a header) file. Agree it is evil in a header file.
JaredPar
@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.)
GMan
@GMan, clarified the answer
JaredPar
@Jared: Looks good to me.
GMan
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
@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
@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
+11  A: 

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.

Kirill V. Lyadvinsky
+1  A: 

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