vectors

can some tell me if im doing vector right as in the private member . im very confused how vectors work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include <iostream>
#include <string>
using namespace std;

class Student
{
   private:
    string sname;
    int sID;
    vector<string>coursetype;

    
}
    

/*
Attributes are:
 student name (just use a single name if you are not clear about getline issues)
 student id number (an integer)
 A list of courses represented by a vector of Course type */
Last edited on
Yes, you've declared that correctly. However:

1) You haven't included the header file that defines std::vector.

2) The standard advice to avoid using namespace std applies here, especially if that class definition is going into a header file.

2) The standard advice to avoid using namespace std applies here, especially if that class definition is going into a header file.


Why specifically if the class declaration goes into a headerfile? What's the difference for when it is and when it isn't going into a headerfile?
@TIM's link says most of what there is to say about this. To phrase it differently, the C++ Core Guidelines has this:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf6-use-using-namespace-directives-for-transition-for-foundation-libraries-such-as-std-or-within-a-local-scope-only
See also SF.7:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf7-dont-write-using-namespace-at-global-scope-in-a-header-file

It's not mentioned in either of those links, but using X and using namespace Y opts into argument-dependent lookup (ADL), which can introduce its own subtle problems.

Some of us (me) prefer never to write using namespace at all, and reserve using X to opt-in for ADL, but I suppose as long as the usage is local - i.e., one makes sure it doesn't affect too much code - it's an acceptable decision.
Last edited on
Topic archived. No new replies allowed.