C++ STL vector Question

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

    C++ STL vector Question

    Hello:

    I am having trouble getting a vector of a class to work from another class

    //*************** *****
    class CaClass{

    int idata;

    CaClass(){idata =1;}
    }
    //*************** *****
    //*************** *****
    class CanotherClass{

    vector <CaClass> aClass;

    int iotherdata;
    }
    //*************** *****

    main()
    {
    CanotherClass anotherClass;
    anotherClass->aClass.push_ba ck(CaClass); //compiles fine but this
    fails
    }
    //*************** *************** *************** ************

    Any ideas?
    Know of any example that demo a vector used from within another class?

    Thank you.

    Drew


  • Buster

    #2
    Re: C++ STL vector Question

    Drew wrote:[color=blue]
    > Hello:
    >
    > I am having trouble getting a vector of a class to work from another class
    >
    > //*************** *****
    > class CaClass{
    >
    > int idata;
    >
    > CaClass(){idata =1;}
    > }
    > //*************** *****
    > //*************** *****
    > class CanotherClass{
    >
    > vector <CaClass> aClass;
    >
    > int iotherdata;
    > }
    > //*************** *****
    >
    > main()
    > {
    > CanotherClass anotherClass;
    > anotherClass->aClass.push_ba ck(CaClass); //compiles fine but this
    > fails[/color]

    It shouldn't compile. CaClass is a type (or class) name. You must
    provide a value (or object) to push_back. Besides, aClass is private in
    CanotherClass. And main always returns int, always.

    [color=blue]
    > }
    > //*************** *************** *************** ************
    >
    > Any ideas?
    > Know of any example that demo a vector used from within another class?
    >
    > Thank you.
    >
    > Drew
    >
    >[/color]

    Correcting your syntax and getting rid of those stupid confusing names:

    #include <vector>

    class X
    {
    int i;
    X () : i (1) { }
    };

    class Y
    {
    public:
    std::vector <X> v;
    int i;
    };

    int main ()
    {
    Y y;
    y->v.push_back (X ());
    }

    Easy enough. "X ()" means "a temporary, default-constructed instance of X".

    Regards,
    Buster.

    Comment

    • Chris \( Val \)

      #3
      Re: C++ STL vector Question


      "Drew" <pescorp@bellso uth.net> wrote in message
      news:pS01c.1512 $JN2.224@bignew s4.bellsouth.ne t...
      | Hello:
      |
      | I am having trouble getting a vector of a class to work from another class
      |
      | //*************** *****
      | class CaClass{
      |
      | int idata;
      |
      | CaClass(){idata =1;}
      | }
      | //*************** *****
      | //*************** *****
      | class CanotherClass{
      |
      | vector <CaClass> aClass;
      |
      | int iotherdata;
      | }
      | //*************** *****
      |
      | main()
      | {
      | CanotherClass anotherClass;
      | anotherClass->aClass.push_ba ck(CaClass); //compiles fine but this
      | fails
      | }
      | //*************** *************** *************** ************
      |
      | Any ideas?
      | Know of any example that demo a vector used from within another class?


      You have many syntax errors in your code.

      Do you have an appropriate book to learn from ?

      Study the following:

      class CaClass
      {
      int idata;
      public:
      CaClass( int n ) : idata( n ) {}
      };

      class CanotherClass
      {
      public:
      std::vector<CaC lass> aClass;
      };

      int main()
      {
      CanotherClass anotherClass;
      anotherClass.aC lass.push_back( CaClass( 10 ) );

      // Note that main() must have an return type
      // specification of 'int'.

      return 0;
      }

      Cheers.
      Chris Val


      Comment

      • Leor Zolman

        #4
        Re: C++ STL vector Question

        On Tue, 2 Mar 2004 08:33:27 -0600, "Drew" <pescorp@bellso uth.net> wrote:
        [color=blue]
        >Hello:
        >
        >I am having trouble getting a vector of a class to work from another class[/color]

        Hi,
        It would help a lot if you cut-and-pasted the actual code, because this is
        chock-full of syntax errors that keep it miles from compiling.

        Let's add headers:

        #include <iostream>
        #include <vector>
        using namespace std; // for testing, usually bad at file scope
        [color=blue]
        >
        >//*************** *****
        >class CaClass{
        >
        >int idata;
        >[/color]
        public:
        [color=blue]
        >CaClass(){idat a =1;}
        >}[/color]

        Missing semicolon after the }

        And the most common style is to put the public first, then the private.
        Note that the default in a class is /private/, so you had /everything/ be
        private, in both classes, which was one of your major issues.

        [color=blue]
        >//*************** *****
        >//*************** *****
        >class CanotherClass{
        >
        >vector <CaClass> aClass;
        >
        >int iotherdata;
        >}
        >//*************** *****[/color]

        Another missing semi, and everything is private. Actually, making all your
        data private is /good/, but you then need some public functions to provide
        an interface to your clients, and your code below would have to be
        modified accordingly.
        [color=blue]
        >
        >main()[/color]

        int main()
        [color=blue]
        >{
        > CanotherClass anotherClass;
        > anotherClass->aClass.push_ba ck(CaClass); //compiles fine but this
        >fails[/color]

        First of all, anotherClass is not a pointer, and CaClass is not an object.
        So if everything were public, you'd have to say:

        anotherClass.aC lass.push_back( CaClass());

        To instantiate an anonymous CaClass object and add it to the vector.


        Add:
        return 0;
        [color=blue]
        >}
        >//*************** *************** *************** ************
        >
        >Any ideas?[/color]
        Heh.
        [color=blue]
        >Know of any example that demo a vector used from within another class?[/color]

        Yours, once fixed.
        [color=blue]
        >
        >Thank you.[/color]
        You're welcome,
        -leor
        [color=blue]
        >
        >Drew
        >[/color]

        Leor Zolman
        BD Software
        leor@bdsoft.com
        www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
        C++ users: Download BD Software's free STL Error Message
        Decryptor at www.bdsoft.com/tools/stlfilt.html

        Comment

        • AngleWyrm

          #5
          Re: C++ STL vector Question

          "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
          news:c226ar$1ol 35t$1@ID-110726.news.uni-berlin.de...
          [color=blue]
          > // Note that main() must have an return type
          > // specification of 'int'.
          >
          > return 0;[/color]

          main() is required to return an int value, but that int value defaults
          to zero, and thus need not be specified. Here is a quote from Bjarne
          Stroustrup, author of "The C++ Programming Language", page 46:

          " The int value returned by main(), if any, is the program's return
          value to "the system." If no value is returned, the system will
          receive a value indicating successful completion. A nonzero value from
          main() indicates failure. "

          That quote is immediately followed by a hello, world example:
          #include <iostream>
          int main()
          {
          std::cout << "Hello, world!\n";
          }

          Thus, in the case of the requisite function main(), there is an
          implicit "return 0;" at the closing brace.


          Comment

          • Mike Wahler

            #6
            Re: C++ STL vector Question


            "AngleWyrm" <no_spam_anglew yrm@hotmail.com > wrote in message
            news:Kaf1c.2799 7$ko6.287431@at tbi_s02...[color=blue]
            > "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
            > news:c226ar$1ol 35t$1@ID-110726.news.uni-berlin.de...
            >[color=green]
            > > // Note that main() must have an return type
            > > // specification of 'int'.
            > >
            > > return 0;[/color]
            >
            > main() is required to return an int value, but that int value defaults
            > to zero, and thus need not be specified.[/color]

            Please read again what Chris wrote. He did *not* say that
            the return statement is required. He was talking about
            'main()'s return type. I.e. it must be

            int main()

            rather than

            main()

            [color=blue]
            >Here is a quote from Bjarne
            > Stroustrup, author of "The C++ Programming Language", page 46:
            >
            > " The int value returned by main(), if any, is the program's return
            > value to "the system." If no value is returned, the system will
            > receive a value indicating successful completion. A nonzero value from
            > main() indicates failure. "
            >
            > That quote is immediately followed by a hello, world example:
            > #include <iostream>
            > int main()
            > {
            > std::cout << "Hello, world!\n";
            > }
            >
            > Thus, in the case of the requisite function main(), there is an
            > implicit "return 0;" at the closing brace.[/color]

            Many (including myself, and apparently Chris as well) feel that
            although it's not mandatory, an explicit return is 'good style'.
            YMMV.

            -Mike


            Comment

            • Leor Zolman

              #7
              Re: C++ STL vector Question

              On Wed, 03 Mar 2004 07:20:10 GMT, "Mike Wahler" <mkwahler@mkwah ler.net>
              wrote:
              [color=blue][color=green]
              >>
              >> Thus, in the case of the requisite function main(), there is an
              >> implicit "return 0;" at the closing brace.[/color]
              >
              >Many (including myself, and apparently Chris as well) feel that
              >although it's not mandatory, an explicit return is 'good style'.
              >YMMV.
              >[/color]

              Me three. That return 0 at the end is like a comment saying, "If we reach
              this point, everything is under control.". OTOH, if I don't see any return
              statement, my immediate thought is: "I have no idea whether or not things
              have settled down in this program; Have any un-recoverable errors
              happened? If so, has some reasonable action been taken? I'd better go read
              through all the code to find out, because I'd hate to have it fall off the
              end of main() with things in a .5-donkeyed state of some kind only to have
              it return 0 by default and fake out the system..."

              With the return 0 there, I can infer the programmer at least /thought/
              his/her i's are dotted and t's crossed when execution reaches that point...
              -leor

              [color=blue]
              >-Mike
              >[/color]

              Leor Zolman
              BD Software
              leor@bdsoft.com
              www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
              C++ users: Download BD Software's free STL Error Message
              Decryptor at www.bdsoft.com/tools/stlfilt.html

              Comment

              • Chris \( Val \)

                #8
                Re: C++ STL vector Question


                "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
                news:KGf1c.1927 5$aT1.2147@news read1.news.pas. earthlink.net.. .
                |
                | "AngleWyrm" <no_spam_anglew yrm@hotmail.com > wrote in message
                | news:Kaf1c.2799 7$ko6.287431@at tbi_s02...
                | > "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
                | > news:c226ar$1ol 35t$1@ID-110726.news.uni-berlin.de...
                | >
                | > > // Note that main() must have an return type
                | > > // specification of 'int'.
                | > >
                | > > return 0;
                | >
                | > main() is required to return an int value, but that int value defaults
                | > to zero, and thus need not be specified.
                |
                | Please read again what Chris wrote. He did *not* say that
                | the return statement is required. He was talking about
                | 'main()'s return type. I.e. it must be
                |
                | int main()
                |
                | rather than
                |
                | main()

                [snip]

                Indeed - That is what I meant :-).

                | Many (including myself, and apparently Chris as well) feel that
                | although it's not mandatory, an explicit return is 'good style'.
                | YMMV.

                Spot on :-).

                Thanks Mike.
                Chris Val


                Comment

                Working...