473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template

Why is this code giving error? How do I fix it?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <class A, typename B>
  5. class AClass
  6. {
  7. private:
  8.   vector<A> aVector;
  9. public:  
  10.   void AFunc(A t1)
  11.   {     aVector.push_back(t1);
  12.   }
  13. };
  14.  
  15. class BClass {
  16. private:
  17.     int a,b,c;
  18. public:
  19.     BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
  20. };
  21. int main() {
  22.     int a,b,c;
  23.     AClass.AFunc(BClass (a,b,c));
  24.     return 0;
  25.  
  26. }
Error is:23 missing template arguments before '.' token

regards,

John
Mar 5 '08 #1
3 7222
gpraghuram
1,275 Expert 1GB
Why is this code giving error? How do I fix it?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <class A, typename B>
  5. class AClass
  6. {
  7. private:
  8.   vector<A> aVector;
  9. public:  
  10.   void AFunc(A t1)
  11.   {     aVector.push_back(t1);
  12.   }
  13. };
  14.  
  15. class BClass {
  16. private:
  17.     int a,b,c;
  18. public:
  19.     BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
  20. };
  21. int main() {
  22.     int a,b,c;
  23.     AClass.AFunc(BClass (a,b,c));
  24.     return 0;
  25.  
  26. }
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
Expand|Select|Wrap|Line Numbers
  1.  int a,b,c;
  2. //AClass.AFunc(BClass (a,b,c));
  3. AClass<BClass,int> x;
  4.  
  5.  

Raghuram
Mar 6 '08 #2
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
Expand|Select|Wrap|Line Numbers
  1.  int a,b,c;
  2. //AClass.AFunc(BClass (a,b,c));
  3. AClass<BClass,int> x;
  4.  
  5.  
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.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <class A, typename B>
  5. class AClass
  6. {
  7. private:
  8.   vector<A> aVector;
  9. public:  
  10.   void AFunc(A t1)
  11.   {     aVector.push_back(t1);
  12.   }
  13.   void print(){
  14.        for(int i=0;i<aVector;i++)
  15.            cout<<aVector[i];
  16.   }
  17. };
  18.  
  19. class BClass {
  20. private:
  21.     int a,b,c;
  22. public:
  23.     BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
  24. };
  25. int main() {
  26.    // int a,b,c;
  27.    // AClass.AFunc(BClass (1,2,3));
  28.     AClass<BClass,int> x;
  29.     print();
  30.     return 0;}
  31.  
  32. /*
  33. template <class A, typename B>
  34. void AClass::print() {
  35.       for(int i=0;i<aVector;i++)
  36.            cout<<aVector[i];
  37. }
  38. */
Mar 6 '08 #3
Laharl
849 Expert 512MB
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.
Mar 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.