Template

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

    Template

    Why is this code giving error? How do I fix it?
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    template <class A, typename B>
    class AClass
    {
    private:
      vector<A> aVector;
    public:  
      void AFunc(A t1)
      {     aVector.push_back(t1);
      }
    };
    
    class BClass {
    private:
    	int a,b,c;
    public:
    	BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
    };
    int main() {
        int a,b,c;
        AClass.AFunc(BClass (a,b,c));
        return 0;
    
    }
    Error is:23 missing template arguments before '.' token

    regards,

    John
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by JohnSmith70
    Why is this code giving error? How do I fix it?
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    template <class A, typename B>
    class AClass
    {
    private:
      vector<A> aVector;
    public:  
      void AFunc(A t1)
      {     aVector.push_back(t1);
      }
    };
    
    class BClass {
    private:
    	int a,b,c;
    public:
    	BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
    };
    int main() {
        int a,b,c;
        AClass.AFunc(BClass (a,b,c));
        return 0;
    
    }
    Error is:23 missing template arguments before '.' token

    regards,

    John

    You are trying to call the template method without specifying the type for the template.
    This code would compile.I changed a declartion in main
    [code=cpp]
    int a,b,c;
    //AClass.AFunc(BC lass (a,b,c));
    AClass<BClass,i nt> x;

    [/code]


    Raghuram

    Comment

    • JohnSmith70
      New Member
      • Oct 2007
      • 22

      #3
      Originally posted by gpraghuram
      You are trying to call the template method without specifying the type for the template.
      This code would compile.I changed a declartion in main
      [code=cpp]
      int a,b,c;
      //AClass.AFunc(BC lass (a,b,c));
      AClass<BClass,i nt> x;

      [/code]
      Raghuram
      Thanks for your reply Raghura, it compiles :)

      However the function AFunc() has been removed, so as I understand, nothing is push_back() into the vector. I've tried to display the contents of the vector, but it doesn't work.
      How can I call the print function of the AClass from main, also I want to be able to push values into the vector from main. To make things simpler for now I am trying to push the values 1,2,3. But since the fields are private I have to use the AFunc() method. Please help me make this compile.



      Code:
      #include <iostream>
      #include <vector>
      using namespace std;
      template <class A, typename B>
      class AClass
      {
      private:
        vector<A> aVector;
      public:  
        void AFunc(A t1)
        {     aVector.push_back(t1);
        }
        void print(){
             for(int i=0;i<aVector;i++)
                 cout<<aVector[i];
        }
      };
       
      class BClass {
      private:
          int a,b,c;
      public:
          BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
      };
      int main() {
         // int a,b,c;
         // AClass.AFunc(BClass (1,2,3));
          AClass<BClass,int> x;
          print();
          return 0;}
      
      /*
      template <class A, typename B>
      void AClass::print() {
            for(int i=0;i<aVector;i++)
                 cout<<aVector[i];
      }
      */

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        First off, always pass class objects to functions by reference to avoid costly (time-wise) copy constructor calls. Second, if you want to print the elements of the vector with <<, you'll have to overload the operator. Google is your friend here, since you'll need to make sure that any templated types you put in to AClass also overload the operator to use it effectively.

        Comment

        Working...