vector data members

This is a discussion on vector data members within the C++ Programming forums, part of the General Programming Boards category; Ok... I must have unlearned something. Why is that the following code gives me an error pointing to the vector ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383

    vector data members

    Ok... I must have unlearned something.
    Why is that the following code gives me an error pointing to the vector declaration?

    Code:
    #ifndef COUNTER_H
    #define COUNTER_H
    
    #include <vector>
    #include <iterator>
    
    class counter {
    public:
        /*...*/
    private:
        /* ... */
        static vector<double> ravg_; //error C2143: syntax error : missing ';' before '<'
    };
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Perhaps you should use the fully qualified std::vector<double> ?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    oh my!

    Thanks laserlight
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Alternatively, you could just declare "using namespace std" in your header file, so you don't need to prefix everything with std::

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Alternatively, you could just declare "using namespace std" in your header file, so you don't need to prefix everything with std::
    Bad idea, the contextual clues (i.e. header guards) tell us this is a header file we are dealing with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by laserlight
    Bad idea, the contextual clues (i.e. header guards) tell us this is a header file we are dealing with.
    So? Why does that prevent you from declaring a namespace? I'm not asking to be rude, I'd actually like to know because whenever I use a std::string or std::vector, I just declare the namespace so I don't need the std:: prefix all the time.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by maxhavoc
    So? Why does that prevent you from declaring a namespace? I'm not asking to be rude, I'd actually like to know because whenever I use a std::string or std::vector, I just declare the namespace so I don't need the std:: prefix all the time.
    because whatever file includes that header file forces the using declaration, you should use only use std:: explicitly in the header file instead of blanketing it.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by indigo0086
    because whatever includes that header file forces the using declaration, you should use only use std:: explicitly in the header file instead of blanketing it.
    But is that really a problem? I mean logically if you're using std::strings or whatever in a header file, you're also using it in the accompanying source file, in which case declaring the using namespace std in the header saves you from having to declare it in the source file.

    Even if you're not using it anywhere else, are there problems caused by saying you're using the std namespace?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Why does that prevent you from declaring a namespace?
    Well, a using declaration does not declare a namespace, but rather brings the names in the namespace (or the given name, if it covers only one name) into the current scope.

    This is problematic in header files, since it means that files that include the header file would also have these names introduced. This pretty much defeats the purpose of namespaces.

    A possible workaround is to have the using directive in a local scope, e.g. in a function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by maxhavoc
    But is that really a problem? I mean logically if you're using std::strings or whatever in a header file, you're also using it in the accompanying source file, in which case declaring the using namespace std in the header saves you from having to declare it in the source file.

    Even if you're not using it anywhere else, are there problems caused by saying you're using the std namespace?
    but what if you don't use "using std::string;" in the main file that also includes the header file. Just because A uses B doesn't mean C has anything to do with it.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Yes. the using directive is best left outside header files. It brings into scope the whole std namespace, which is bound to sooner or later create name collisions. Even if it does not, it is bad style. Scope shoud always be restricted to the best of our abilities to the explicitly declared names in our code.

    I prefer to use the scope operator as laserlight suggested. But a viable option for header files can also be the using declaration. An using declaration such as using std::cout is much safer.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Even if you're not using it anywhere else, are there problems caused by saying you're using the std namespace?
    Imagine an header file that brings into scope the whole of std by means of a using directive (using namespace std).

    Now... this header file defines a class you use on several projects at your workplace. One of your workmates just created a piece of code with a variable name cout (for instance long cout; ). He then goes on to include your header file. Can you see the problem?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I forget which thread it was but there was also something that said if you use a namespace declaration in the parameter list of a function, then the return type and other parameters are automatically declared within a particular namespace. Or something like it. It had some name.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Koenig lookup ?
    I dont think indigo0086 is referring to that, since this mechanism apparently involves using "a namespace declaration in the parameter list of a function", and that certainly isnt ADL.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2008, 11:46 PM
  2. Pointing to struture members.....
    By kas2002 in forum C++ Programming
    Replies: 7
    Last Post: 08-13-2006, 10:07 AM
  3. What members does struct udphdr(linux) have?
    By failure_to in forum C Programming
    Replies: 2
    Last Post: 05-27-2004, 01:07 PM
  4. VC++ IntelliSense (showing class members)
    By _Elixia_ in forum Tech Board
    Replies: 0
    Last Post: 08-02-2003, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21