Vector in cpp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcelo

    #1

    Vector in cpp

    Hello,

    I would like to know how to use the std::vector variable.
    I have tried a class something like this, but I get an error and I don't know why...
    the error
    lib/image.h:13: error: using-declaration for non-member at class scope
    lib/image.h:13: error: expected ‘;’ before ‘<’ token

    the code
    #ifndef IMAGE_H
    #define IMAGE_H

    #include <stdio.h>

    /*Create the class for the images */
    class Image {
    public:
    int w; // The width of the image.
    int h; // The height of the image.
    int dim; // The dimension of the the image.
    //The array RGB type uchar of the image.
    std::vector<uns igned char> arr;
    };

    #endif /* IMAGE_H */
  • roberts.noah@gmail.com

    #2
    Re: Vector in cpp


    Marcelo wrote:[color=blue]
    > Hello,
    >
    > I would like to know how to use the std::vector variable.
    > I have tried a class something like this, but I get an error and I don't know why...
    > the error
    > lib/image.h:13: error: using-declaration for non-member at class scope
    > lib/image.h:13: error: expected ';' before '<' token
    >
    > the code
    > #ifndef IMAGE_H
    > #define IMAGE_H
    >
    > #include <stdio.h>[/color]
    #include <vector> // I would also change above to cstdio or use c++
    streams.[color=blue]
    >
    > /*Create the class for the images */
    > class Image {
    > public:
    > int w; // The width of the image.
    > int h; // The height of the image.
    > int dim; // The dimension of the the image.
    > //The array RGB type uchar of the image.
    > std::vector<uns igned char> arr;
    > };
    >
    > #endif /* IMAGE_H */[/color]

    Comment

    • Marcelo

      #3
      Re: Vector in cpp

      I suppose that i need this, because then the compilation works just fine.

      #include <vector.h>

      However, where can I have more documentation about vector type?

      thanks a lot

      MArcelo

      Comment

      • W Marsh

        #4
        Re: Vector in cpp

        On Wed, 14 Dec 2005 18:25:40 +0100, Marcelo <mache_1999@yah oo.com>
        wrote:
        [color=blue]
        >Hello,
        >
        >I would like to know how to use the std::vector variable.
        >I have tried a class something like this, but I get an error and I don't know why...
        >the error
        >lib/image.h:13: error: using-declaration for non-member at class scope
        >lib/image.h:13: error: expected ‘;’ before ‘<’ token
        >
        >the code
        >#ifndef IMAGE_H
        >#define IMAGE_H
        >
        >#include <stdio.h>
        >
        >/*Create the class for the images */
        >class Image {
        > public:
        > int w; // The width of the image.
        > int h; // The height of the image.
        > int dim; // The dimension of the the image.
        > //The array RGB type uchar of the image.
        > std::vector<uns igned char> arr;
        >};
        >
        >#endif /* IMAGE_H */[/color]

        You forgot #include <vector>.

        Also, stdio.h isn't very C++. Are you still using printf? Consider the
        alternatives C++ offers you.

        Comment

        • roberts.noah@gmail.com

          #5
          Re: Vector in cpp


          Marcelo wrote:[color=blue]
          > I suppose that i need this, because then the compilation works just fine.
          >
          > #include <vector.h>[/color]

          You are using old, prestandard headers. Leave out the .h

          Get "The C++ Standard Library" by Josuttis for documentation.

          Comment

          • osmium

            #6
            Re: Vector in cpp

            "Marcelo" writes:
            [color=blue]
            > However, where can I have more documentation about vector type?[/color]

            The first two Google hits on <stl reference> are the two main on-line
            references, Dinkumware and SGI (nee Sun). Being reference there are pretty
            cryptic but there is a lot more on Google you can probably find as easily
            for yourself.


            Comment

            • Mike Smith

              #7
              Re: Vector in cpp

              W Marsh wrote:[color=blue]
              >
              > Also, stdio.h isn't very C++. Are you still using printf? Consider the
              > alternatives C++ offers you.[/color]

              C++ itself doesn't really offer a good alternative to printf(), IMO.
              Boost does, however.

              --
              Mike Smith

              Comment

              • Default User

                #8
                Re: Vector in cpp

                Mike Smith wrote:
                [color=blue]
                > W Marsh wrote:[color=green]
                > >
                > > Also, stdio.h isn't very C++. Are you still using printf? Consider
                > > the alternatives C++ offers you.[/color]
                >
                > C++ itself doesn't really offer a good alternative to printf(), IMO.[/color]

                I agree, I still use printf when I need a compact way to do formatted
                output. I'm not sure what the objection is.
                [color=blue]
                > Boost does, however.[/color]

                Bah, third party, off-topic library :)



                Brian

                Comment

                • Mike Smith

                  #9
                  Re: Vector in cpp

                  Default User wrote:[color=blue]
                  > Mike Smith wrote:
                  >
                  >[color=green]
                  >>W Marsh wrote:
                  >>[color=darkred]
                  >>>Also, stdio.h isn't very C++. Are you still using printf? Consider
                  >>>the alternatives C++ offers you.[/color]
                  >>
                  >>C++ itself doesn't really offer a good alternative to printf(), IMO.[/color]
                  >
                  >
                  > I agree, I still use printf when I need a compact way to do formatted
                  > output. I'm not sure what the objection is.[/color]

                  Well, apart from being seen as gauche by the "new C++" crowd, it is kind
                  of a pain. Suppose you want to generate a string formatted
                  printf()-style, to do it using otherwise C++-ish style you need to jump
                  through hoops:

                  int a = 5;
                  float b = 3.14;
                  string c = "blah";

                  char tmpbuf[HOW_MUCH_TO_ALL OCATE_HERE];
                  sprintf(tmpbuf, "a = %08d, b = %.2f, c = %s", a, b, c.c_str());
                  string s(tmpbuf);

                  It's ironic to have to use character arrays for this sort of thing, when
                  one of the very reasons that C++ strings were introduced in the first
                  place was... to eliminate the need to work with character arrays. :-/

                  --
                  Mike Smith

                  Comment

                  • baalbek

                    #10
                    Re: Vector in cpp

                    Come on, dude!

                    std::ostringsre am is way better than any printf!

                    You just need to include the right headers!

                    What's better:

                    printf("%s %d", "test", 12);

                    or this:

                    std::ostringstr eam o;
                    cout << o << "test" << 12;

                    Baalbek


                    Mike Smith wrote:[color=blue]
                    > W Marsh wrote:
                    >[color=green]
                    >>
                    >> Also, stdio.h isn't very C++. Are you still using printf? Consider the
                    >> alternatives C++ offers you.[/color]
                    >
                    >
                    > C++ itself doesn't really offer a good alternative to printf(), IMO.
                    > Boost does, however.
                    >
                    > --
                    > Mike Smith[/color]

                    Comment

                    • Neil Cerutti

                      #11
                      Re: Vector in cpp

                      On 2005-12-16, baalbek <rcs@bgoark.n o> wrote:[color=blue]
                      > Come on, dude!
                      >
                      > std::ostringsre am is way better than any printf!
                      >
                      > You just need to include the right headers!
                      >
                      > What's better:
                      >
                      > printf("%s %d", "test", 12);
                      >
                      > or this:
                      >
                      > std::ostringstr eam o;
                      > cout << o << "test" << 12;[/color]

                      Now write iostream code to do this:

                      printf("%10.10f %f %.0f" 1000.48702, 23.718, 800.0);

                      --
                      Neil Cerutti

                      Comment

                      • Marcelo Pinto

                        #12
                        Re: Vector in cpp


                        baalbek escreveu:
                        [color=blue]
                        > Come on, dude!
                        >
                        > std::ostringsre am is way better than any printf!
                        >
                        > You just need to include the right headers!
                        >
                        > What's better:
                        >
                        > printf("%s %d", "test", 12);
                        >
                        > or this:
                        >
                        > std::ostringstr eam o;
                        > cout << o << "test" << 12;[/color]
                        You forgot the white space after *test*.

                        The thing I dislike in printf (and family) is:

                        printf("%d %s", "test ", 12);

                        The invertion is not clear and the results may be surprising.

                        HTH,

                        Marcelo Pinto

                        Comment

                        • Neil Cerutti

                          #13
                          Re: Vector in cpp

                          On 2005-12-16, Marcelo Pinto <mpinto70@gmail .com> wrote:[color=blue]
                          >
                          > baalbek escreveu:
                          >[color=green]
                          >> Come on, dude!
                          >>
                          >> std::ostringsre am is way better than any printf!
                          >>
                          >> You just need to include the right headers!
                          >>
                          >> What's better:
                          >>
                          >> printf("%s %d", "test", 12);
                          >>
                          >> or this:
                          >>
                          >> std::ostringstr eam o;
                          >> cout << o << "test" << 12;[/color]
                          > You forgot the white space after *test*.
                          >
                          > The thing I dislike in printf (and family) is:
                          >
                          > printf("%d %s", "test ", 12);
                          >
                          > The invertion is not clear and the results may be surprising.[/color]

                          Yup. fprintf and friends provide an excellent and convenient
                          notation, but they are not type safe or extensible. iostreams
                          provide for type-safe and extensible formatting, at the expense
                          of convenient notation.

                          On the other hand, extensibility means that a convenient notation
                          is possible to implement yourself, as boost did.

                          --
                          Neil Cerutti

                          Comment

                          Working...