Vector Template of type Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnSmith70
    New Member
    • Oct 2007
    • 22

    #1

    Vector Template of type Class

    How do I write a template with three typename variables, so that from main I can use that template as a vector, to add and later go back or go forward in the vector and then view the content of the vector in that location?

    Here what I've came up with

    Code:
    template <typename avar, typename bvar, typename cvar>
    class AClass {
    
    public:
    	avar var1;
    	bvar var2;
    	cvar var3;
    	AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
    };
    
    //main program
    vector <AClass> avector;
    I want to use the AClass, as the template of adding to the vector. I am trying to write three functions, one to view the vector, one to go forward in the vector and one to go back. I would really appreciate your help?


    Thanks,

    John
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A template is a pattern. You cannot have a vector of patterns. You can only have a vector of objects. To get objects, you need tpo specify the types for your template.
    [code=cpp]
    vector <AClass<int,str ing, float> > avector;
    [/code]

    This compiles and links using your template withourt any changes.

    BTW: Bw sure to have a space so you have > > rather than >> in your vector definition (like I did above). The >> is the insertion operator and will confuse the compiler.

    Comment

    • JohnSmith70
      New Member
      • Oct 2007
      • 22

      #3
      thanks for your reply,

      I have modified it now, but it still gets an error. I haven't been able to write the function to go back in the vector. Here is my code:
      Code:
      template <typename avar, typename bvar, typename cvar>
      class AClass {
       
      public:
          avar var1;
          bvar var2;
          cvar var3;
          AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}};
      
      vector <AClass<int,int, int> > avector;
      
      int main() {     
          avector.push_back(); 
      
           void forward(const AClass &a) 
           {
            //get current position and go plus 1
           }
      
           int back(const AClass &a) 
           {
            //get current position and go back 1
           //display content of current position
           }
      }
      On compile I unfortunately get an error. Why is it not working, and how can I write the functions to work with the vector?

      regards,

      John

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        First, your vector should be inside main() and not outside. Avoid using global variables.
        Second, you have writtwn two functions inside main(). You can't do that. You call the functions from inside main() but you code them outside of main.
        Third, a template used as a function argument cannot be AClass. Remember AClass is a pattern. It is not a type. It won't be a type until you specialize the template. The function argument needs to be AClass<int, int, int>&.
        Fourth, you have a push_back() in main() that has no arguments.
        push_back() always requires the thing you are pushing into the vector.

        Other than that, you code compiles OK.

        Comment

        • JohnSmith70
          New Member
          • Oct 2007
          • 22

          #5
          Remember AClass is a pattern. It is not a type. It won't be a type until you specialize the template. The function argument needs to be AClass<int, int, int>&.
          I've tried to make the changes as you have suggested. But I don't know how to specialize the template as you have said.

          Here is the code:
          Code:
          template <typename avar, typename bvar, typename cvar>
          class AClass {
           
          public:
              avar var1;
              bvar var2;
              cvar var3;
              AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
           
               void forward(const AClass &a) 
               {
                //get current position and go plus 1
               }
               int back(const AClass &a) 
               {
                //get current position and go back 1
               //display content of current position
               }
          };
          
          int main() {     
           
           vector <AClass<int,int,int>& > avector;
           avector.push_back(1,2,3);    
          }
          Errors are:
          In function `int main()':
          27 `vector' undeclared (first use this function)
          (Each undeclared identifier is reported only once for each function it appears in.)
          27 expected primary-expression before '>' token
          27 `avector' undeclared (first use this function)

          I would really appreciate your generous help.

          Regards,

          John

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            You've got a few issues here. First, did you #include <vector> so that the compiler knows what a vector is? Second, your push_back() call has 3 integers as a parameter while your vector holds AClass<int, int, int> objects. Thus, you can only push back objects of type AClass<int, int, int>. You're pushing (int, int int), which is completely different.

            Comment

            • JohnSmith70
              New Member
              • Oct 2007
              • 22

              #7
              Thanks for your response dear friend. Here is my code:
              Code:
              #include <iostream>
              #include <vector>
              using namespace std;
              
              template <typename avar, typename bvar, typename cvar>
              class AClass {
               
              public:
                  avar var1;
                  bvar var2;
                  cvar var3;
                  AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
               
                   void forward(const AClass &a) 
                   {
                    //get current position and go plus 1
                   }
                   int back(const AClass &a) 
                   {
                    //get current position and go back 1
                   //display content of current position
                   }
              };
              
              int main() {     
               
               vector <AClass<int,int,int> > avector;
               avector.push_back(1,2,3); 
              }
              Originally posted by Laharl
              Second, your push_back() call has 3 integers as a parameter while your vector holds AClass<int, int, int> objects. Thus, you can only push back objects of type AClass<int, int, int>. You're pushing (int, int int), which is completely different.
              I don't know how to do that. How can I push back objects of type AClass<int, int, int>?

              Thanks,

              John

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                You can push back objects of type AClass<int, int, int> by creating them first. You have a constructor for the class, so you create objects using it, then you push_back those initialized objects.

                Comment

                • JohnSmith70
                  New Member
                  • Oct 2007
                  • 22

                  #9
                  Originally posted by Laharl
                  You can push back objects of type AClass<int, int, int> by creating them first. You have a constructor for the class, so you create objects using it, then you push_back those initialized objects.
                  Code:
                  #include <iostream>
                  #include <vector>
                  
                  using namespace std;
                  
                  template <typename avar, typename bvar, typename cvar>
                  class AClass {
                   
                  public:
                      avar var1;
                      bvar var2;
                      cvar var3;
                      AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
                   
                       void forward(const AClass &a) 
                       {
                        //get current position and go plus 1
                       }
                       int back(const AClass &a) 
                       {
                        //get current position and go back 1
                       //display content of current position
                       }
                  };
                  
                  int main() {     
                   vector <AClass<int,int,int> > avector;
                   AClass<int, int, int> obj1(1,2,3);
                   avector.push_back(obj1);  
                  }
                  Still won't work, help please

                  John

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    You're using the initializer list wrong. It should be written such that you're instantiating the member variables. As such, you call their constructors with the membername(args ) syntax.

                    [CODE=cpp]
                    class foo{
                    private:
                    int foo;
                    std::string bar;
                    public:
                    public foo(const int& f, const std::string& b): foo(f), bar(b){}
                    };
                    [/CODE]

                    Comment

                    • JohnSmith70
                      New Member
                      • Oct 2007
                      • 22

                      #11
                      I've tried as you said, and now it compiles, thanks! :). But how do I get it to view the contents of the vector? Here what I've done:
                      Code:
                      #include <iostream>
                      #include <vector>
                      using namespace std;
                      
                      template <typename avar, typename bvar, typename cvar>
                      class AClass
                      {
                      public:
                        avar var1;
                        bvar var2;
                        cvar var3;
                        AClass (const avar& v1, const bvar& v2, const cvar& v3) 
                          : var1(v1), var2(v2), var3(v3) {}
                          
                        void forward(const AClass &a) 
                        {
                            //get current position and go plus 1
                        }
                        int back(const AClass &a) 
                        {
                            //get current position and go back 1
                           //display content of current position
                        }
                      };
                      
                      int main()
                      {    
                       vector <AClass<int,int,int> > avector;
                       AClass<int, int, int> obj1(1,2,3);
                       avector.push_back(obj1);
                      }
                      This doesn't work. Please help, I would really appreciate it.
                      Code:
                      display()
                        {
                                 for( int i = 0; i < avector.size(); i++ )    
                                  cout << avector[i] << " ";
                        }
                      John

                      Comment

                      • Laharl
                        Recognized Expert Contributor
                        • Sep 2007
                        • 849

                        #12
                        That's because the << operator doesn't know how to deal with AClass objects of whatever type. You can overload the operator, but if you don't know about friend functions, don't bother. Just use the . operator to access the three integers, since you declared them as public.

                        Comment

                        • JohnSmith70
                          New Member
                          • Oct 2007
                          • 22

                          #13
                          Originally posted by Laharl
                          That's because the << operator doesn't know how to deal with AClass objects of whatever type. You can overload the operator, but if you don't know about friend functions, don't bother. Just use the . operator to access the three integers, since you declared them as public.
                          Thank you very much Laharl for so many of the responses, I really appreciate it. After so many questions I am embarassed to ask, how I can do what you said. Can you show the syntax or the necessary implementation to display the contents of the vector?

                          regards,

                          John

                          Comment

                          • Laharl
                            Recognized Expert Contributor
                            • Sep 2007
                            • 849

                            #14
                            Go read this. It'll make more sense than any explanation I could give (and is far more thorough).

                            Comment

                            • JohnSmith70
                              New Member
                              • Oct 2007
                              • 22

                              #15
                              Originally posted by Laharl
                              Go read this. It'll make more sense than any explanation I could give (and is far more thorough).
                              Hi, finally I've managed to display the contents, but it seems wrong.

                              Code:
                              #include <iostream>
                              #include <vector>
                              using namespace std;
                              
                              template <typename avar, typename bvar, typename cvar>
                              class AClass
                              {
                              public:
                                avar var1;
                                bvar var2;
                                cvar var3;
                                AClass (const avar& v1, const bvar& v2, const cvar& v3) 
                                  : var1(v1), var2(v2), var3(v3) {}
                                  
                                void forward(const AClass &a) 
                                {
                                     //get current position and go plus 1
                                     //avector.push_back();
                                }
                                int back(const AClass &a) 
                                {
                                    //get current position and go back 1
                                   //display content of current position
                                  //avector.pop_back();
                                }
                              };
                              
                              int main()
                              {    
                               vector <AClass<int,int,int> > avector;
                               AClass<int, int, int> obj1(1,2,3);
                                
                               avector.push_back(obj1); //how can I use he function forward(obj1) instead?
                                for( int i = 0; i < avector.size(); i++ ){
                                      cout<<obj1.var1;
                                      cout<<obj1.var2;
                                      cout<<obj1.var3;
                                      cout<<endl;
                              }
                               AClass<int, int, int> obj2(4,5,6);
                               avector.pop_back(); //can I use the function back(obj1) instead?
                                      cout<<obj2.var1;
                                      cout<<obj2.var2;
                                      cout<<obj2.var3;
                                      cout<<endl;
                              }
                              How can I go to the the previous slot of the vector in this case? I still haven't manage to write the definitions of the mentioned functions. How can I pass a parameter to the forward function, so that it adds to the vector by the function call?

                              John

                              Comment

                              Working...