0

I've been trying this in C++:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

int add(vector<int>& nums) {

}

But the intelliSense is telling me "identifier vector is undefined". I'm trying to create a function that has a vector passed in as an argument. I'm probably doing something really stupid. This is in a separate C++ file from my main program.

Tiernan Watson
  • 313
  • 2
  • 12
  • try adding `using std::vector;` before `add` – Elan Hickler Dec 17 '15 at 20:38
  • 6
    also, try not to rely so much on intellisense. Sometimes it hangs, sometimes doesn't update in real time. The true test is compilation. Compile it and see the compiler errors. – bolov Dec 17 '15 at 20:38
  • Also, try to read more about `namespaces`. It will help also next time, more than simply adding a `std::` somewhere this time. – skypjack Dec 17 '15 at 20:49

1 Answers1

10

You have to qualify the namespace: std::vector.

Please avoid using namespace where possible, as it can easily lead to naming collisions (and if used in headers, pollute the whole global namespace).

TheOperator
  • 5,936
  • 29
  • 42
  • Thanks. Also when writing a prototype in the header file, it seems strange to include again. Is this the only way to do it? By doing #include and then doing the prototype? – Tiernan Watson Dec 17 '15 at 20:59
  • Assuming you mean a function declaration: yes, you must include `` before you use the identifier `std::vector`, because [standard containers cannot be forward-declared](http://stackoverflow.com/q/307343/5427663). You don't have to include it again in the .cpp file. However, I'd recommend to always include headers if you're not in the corresponding .hpp/.cpp file, because included headers are an implementation detail and can change (and you don't want such changes to break your code). – TheOperator Dec 17 '15 at 21:08
  • So would you say this is well-written code? I have a header file for the add function (just so I can learn these things), containing the one line prototype without body. In it I included and then I included add.h into the add.cpp file with the add function body. Then in my main cpp file I included add.h again. I'm coming from a C# background, so it seems like I'm including too much, but that might just be me. – Tiernan Watson Dec 17 '15 at 21:18
  • 1
    @TiernanWatson A header should be completely self contained and include everything it needs to function. Everything it needs and nothing more. If you have to count on a header being included before your header is included, users of your header (which may include you) wind up with debugging time spent out of proportion with the time spent ensuring the completeness of the header. And once they figure it out, you know who will catch the blame. – user4581301 Dec 17 '15 at 22:15
  • @TiernanWatson Typically for one function you aren't going to bother with a header file. Implement the function above all uses of the function in the same file and you are done. If that one function is used by many other files, now you have a case for a header. In this case you'll usually have more than just one function, you'll have a whole library of functions and datatype definitions to go with them. – user4581301 Dec 17 '15 at 22:22