Problem with push_back,help me ASAP

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

    Problem with push_back,help me ASAP


    hi



    am getting error when am trying to type cast.is it possible to type
    cast from one type of vector to another type.is there any other way
    other than overloading.my peice of code in which am getting error is.



    Here is my peice of code.

    _______________ ______



    vector<Varbind> varbinds;

    vector<NameValu ePair> nameValuePairs;



    if(varbinds.siz e()!=0) {



    for(int index = 0;index<varbind s.size();++inde x) {



    nameValuePairs. push_back(varbi nds[index]);

    }



    }



    here i have to cast the type of varbinds from type vector<Varbinds > to
    the type vector<NameValu ePair>





    And the second query is

    _______________ _______



    it is also with push_back.Here is the peice of code



    vector<NameValu ePair> ar = req.arr();

    VarbindList varbinds;

    for ( int i = 0; i < ar.size(); ++i ) {

    varbinds.push_b ack(ar); //Error Here

    }



    Here varbinds is the object of the class VarbindList and ar is of
    type vector<NameValu ePair>.I have to cast vector<NameValu ePairs> to the
    type VarbindList.



    Please help me in this to aspects ASAP.


    --
    Posted via http://dbforums.com
  • Victor Bazarov

    #2
    Re: Problem with push_back,help me ASAP

    "AnuSree" <member44430@db forums.com> wrote...[color=blue]
    > am getting error when am trying to type cast.is it possible to type
    > cast from one type of vector to another type.is there any other way
    > other than overloading.my peice of code in which am getting error is.
    >
    >
    >
    > Here is my peice of code.
    >
    > _______________ ______
    >
    >
    >
    > vector<Varbind> varbinds;
    >
    > vector<NameValu ePair> nameValuePairs;
    >
    >
    >
    > if(varbinds.siz e()!=0) {
    >
    >
    >
    > for(int index = 0;index<varbind s.size();++inde x) {
    >
    >
    >
    > nameValuePairs. push_back(varbi nds[index]);[/color]

    It is only possible to do this if 'Varbind&' is convertible to
    'NameValuePair const&'.
    [color=blue]
    >
    > }
    >
    >
    >
    > }
    >
    >
    >
    > here i have to cast the type of varbinds from type vector<Varbinds > to
    > the type vector<NameValu ePair>[/color]

    No, you only have to have 'Varbinds' convertible to 'NameValuePair' but
    since you didn't post the definition of either of them, there is no way
    to recommed anything in particular.
    [color=blue]
    > And the second query is
    >
    > _______________ _______
    >
    >
    >
    > it is also with push_back.Here is the peice of code
    >
    >
    >
    > vector<NameValu ePair> ar = req.arr();
    >
    > VarbindList varbinds;[/color]

    What's 'VarbindList'? I'll assume that it's a vector<Varbind> .
    Next time please specify.
    [color=blue]
    >
    > for ( int i = 0; i < ar.size(); ++i ) {
    >
    > varbinds.push_b ack(ar); //Error Here[/color]

    Well, again, you're trying to use 'vector<NameVal uePair>' where
    a 'Varbind' is expected (under my assumption).
    [color=blue]
    >
    > }
    >
    >
    >
    > Here varbinds is the object of the class VarbindList and ar is of
    > type vector<NameValu ePair>.I have to cast vector<NameValu ePairs> to the
    > type VarbindList.[/color]

    No, you don't have to cast to 'VarbindList', you only need it to be
    convertible to the type '.push_back()' expects.

    Next time post the _complete_ code. And take it easy with whitespace,
    will you?

    Victor


    Comment

    • AnuSree

      #3
      Re: Problem with push_back,help me ASAP


      Hi Victor



      Thanks for your trail, here am sending you the VarbindList class
      definition.



      _______________

      Class VarbindList:

      _______________



      #ifndef VARBINDLIST_H

      #define VARBINDLIST_H



      #include "Varbind.h"

      #include "Vector.h"

      #include <vector.h>

      #include "Snmp.h"

      #include "Object.h"



      class VarbindList : public Vector {



      public:

      VarbindList() {



      static const string INVALID_OBJ =
      "Invalid instance";

      static const string INVALID_OBJ_VEC = "Not
      an instance of vector";

      }

      VarbindList(int initialCapacity );



      static VarbindList decodeSequence( Asn1Value
      aVarbinds);// throws



      void addVarbind(Obje ct aVarbind);// throw Exception;





      void addVarbinds(vec tor <Object> aVarbindArray);// throw
      Exception;





      void addVarbinds(Vec tor aVectorOfVarbin ds);// throw Exception;





      void addVarbinds(Obj ect aVarbind);// throw Exception;





      vector <Varbind> getVarbindArray ();

      vector <Object> encode();

      char * toString();



      // Constants

      private:

      static const string INVALID_OBJ;// = "Invalid
      instance";

      static const string INVALID_OBJ_VEC ;// = "Not an
      instance of vector";

      };

      #endif // VARBINDLIST_H



      _______________ ____

      Class NameValuePair:

      _______________ ____



      # ifndef NAMEVALUEPAIR_H

      # define NAMEVALUEPAIR_H



      #include "Snmp.h"

      # include "AllInclude s.h"



      class NameValuePair {

      private:

      string Name;

      string Value;



      public:

      NameValuePair() { }

      NameValuePair(s tring mName, string mValue) {

      Name = mName;

      Value = mValue;

      }

      //Get methods..

      string name() { return Name; }

      string value() { return Value; }



      //Set Methods

      void name(string mName) { Name = mName; }

      void value(string mValue) { Value = mValue; }

      };

      # endif





      And this is the peice of code of a method in which i am getting error
      when am using push_back.



      Message SnmpSouthAdapto r :: encode(Request req) {



      vector<NameValu ePair> ar = req.arr();

      VarbindList varbinds;// = VarbindList();

      for ( int i = 0; i < ar.size(); ++i ) {

      varbinds.push_b ack(ar); //Error Here

      }

      }





      Here in varbinds.push_b ack(ar); i have to type cast the type of ar
      from vector<NameValu ePair> to the type VarbindList but am not getting
      hoe to do this,since the type of the ar is not the same as the type
      of varbind am getting error no matching function for call to

      `VarbindList::p ush_back (NameValuePair &)'





      Here VarbindList is a class and varbinds is the object of the class
      VarbindList and ar is of type vector<NameValu ePair>.



      I wrote the class header files of both VarbindList and NameValuePair on
      the top.Help ASAP


      --
      Posted via http://dbforums.com

      Comment

      Working...