Thread: vector data members

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    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 '<'
    };
    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
    28,413
    Perhaps you should use the fully qualified std::vector<double> ?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    oh my!

    Thanks laserlight
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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
    Ireland
    Posts
    8,446
    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?
    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,005
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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-18-2008, 12:46 AM
  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