Simple Vector Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #1

    Simple Vector Problem

    Hi everyone!
    Until now, I've almost only programmed in Java and am now learning C++ (using Bjarne Stroustrup's book). Now, I wanted to test one of the examples from the book, but somehow it doesn't work as it should.
    Here's my code, reduced to the most simple elements:
    [code=c++]
    #include <iostream>
    #include <stdexcept>

    using namespace std;

    vector<int> myVec(100);

    int main()
    {
    for(int i=0; i<26; i++)
    {
    myVec[i]=i;
    }
    }
    [/code]When trying to compile it with g++, I get: (translated from german)
    Code:
    vectorTest.cpp:12: Error: expected constructor, destructor, or type conversion before »<« token
    vectorTest.cpp: In function »int main()«:
    vectorTest.cpp:23: Error: »myVec« was not defined in this scope
    Here, line 12 is the line "vector<int > myVec(100);" and line 23 is "myVec[i]=i;".
    In Java, I would guess, that a line like
    [code=java]
    myVec = new Vec<int>();
    [/code]was missing, but as it's not in the book, there must be some other error.

    I'm sure, the error is really simple, but I just can't find it. Who will solve the mystery for me?

    Greetings,
    Nepomuk
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You need to #include <vector>.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by Laharl
      You need to #include <vector>.
      I KNEW it would be simple! ^^
      Thanks a lot, it works fine now. Thing is, in this book, he just left out all "#include"s and I just assumed, vector was in one of those, he had mentioned.
      Do you have to include a lot of classes like "vector" separately or are most in libraries like "iostream"?

      Greetings,
      Nepomuk

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You have to #include any STL classes you use, such as set, map, vector, list, etc. Otherwise, most of what you need can be found in iostream or string.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by Ganon11
          You have to #include any STL classes you use, such as set, map, vector, list, etc. Otherwise, most of what you need can be found in iostream or string.
          Thanks a lot, I'll keep that in mind.

          Greetings,
          Nepomuk

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Keep your Bjarne Stroustrup open at page 431 etc. (at least in my old copy); it
            lists the most important header files from the standard library.

            kind regards,

            Jos

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by JosAH
              Keep your Bjarne Stroustrup open at page 431 etc. (at least in my old copy); it
              lists the most important header files from the standard library.

              kind regards,

              Jos
              Thanks, it's the same page in my quite new "Special Edition". Those lists will come in handy next time! ^^

              Greetings,
              Nepomuk

              Comment

              Working...