473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Vector Template of type Class

22 New Member
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

Expand|Select|Wrap|Line Numbers
  1. template <typename avar, typename bvar, typename cvar>
  2. class AClass {
  3.  
  4. public:
  5.     avar var1;
  6.     bvar var2;
  7.     cvar var3;
  8.     AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
  9. };
  10.  
  11. //main program
  12. vector <AClass> avector;
  13.  
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
Mar 2 '08 #1
15 1914
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Expand|Select|Wrap|Line Numbers
  1. vector <AClass<int,string, float> > avector;
  2.  
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.
Mar 2 '08 #2
JohnSmith70
22 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. template <typename avar, typename bvar, typename cvar>
  2. class AClass {
  3.  
  4. public:
  5.     avar var1;
  6.     bvar var2;
  7.     cvar var3;
  8.     AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}};
  9.  
  10. vector <AClass<int,int, int> > avector;
  11.  
  12. int main() {     
  13.     avector.push_back(); 
  14.  
  15.      void forward(const AClass &a) 
  16.      {
  17.       //get current position and go plus 1
  18.      }
  19.  
  20.      int back(const AClass &a) 
  21.      {
  22.       //get current position and go back 1
  23.      //display content of current position
  24.      }
  25. }
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
Mar 3 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Mar 3 '08 #4
JohnSmith70
22 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. template <typename avar, typename bvar, typename cvar>
  2. class AClass {
  3.  
  4. public:
  5.     avar var1;
  6.     bvar var2;
  7.     cvar var3;
  8.     AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
  9.  
  10.      void forward(const AClass &a) 
  11.      {
  12.       //get current position and go plus 1
  13.      }
  14.      int back(const AClass &a) 
  15.      {
  16.       //get current position and go back 1
  17.      //display content of current position
  18.      }
  19. };
  20.  
  21. int main() {     
  22.  
  23.  vector <AClass<int,int,int>& > avector;
  24.  avector.push_back(1,2,3);    
  25. }
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
Mar 3 '08 #5
Laharl
849 Recognized Expert Contributor
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.
Mar 3 '08 #6
JohnSmith70
22 New Member
Thanks for your response dear friend. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template <typename avar, typename bvar, typename cvar>
  6. class AClass {
  7.  
  8. public:
  9.     avar var1;
  10.     bvar var2;
  11.     cvar var3;
  12.     AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
  13.  
  14.      void forward(const AClass &a) 
  15.      {
  16.       //get current position and go plus 1
  17.      }
  18.      int back(const AClass &a) 
  19.      {
  20.       //get current position and go back 1
  21.      //display content of current position
  22.      }
  23. };
  24.  
  25. int main() {     
  26.  
  27.  vector <AClass<int,int,int> > avector;
  28.  avector.push_back(1,2,3); 
  29. }
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
Mar 3 '08 #7
Laharl
849 Recognized Expert Contributor
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.
Mar 3 '08 #8
JohnSmith70
22 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template <typename avar, typename bvar, typename cvar>
  7. class AClass {
  8.  
  9. public:
  10.     avar var1;
  11.     bvar var2;
  12.     cvar var3;
  13.     AClass (const avar& var1, const bvar& var2, const cvar& var3): avar(var1), bvar(var2), cvar(var3) {}
  14.  
  15.      void forward(const AClass &a) 
  16.      {
  17.       //get current position and go plus 1
  18.      }
  19.      int back(const AClass &a) 
  20.      {
  21.       //get current position and go back 1
  22.      //display content of current position
  23.      }
  24. };
  25.  
  26. int main() {     
  27.  vector <AClass<int,int,int> > avector;
  28.  AClass<int, int, int> obj1(1,2,3);
  29.  avector.push_back(obj1);  
  30. }
Still won't work, help please

John
Mar 3 '08 #9
Laharl
849 Recognized Expert Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. class foo{
  2.  private:
  3.   int foo;
  4.   std::string bar;
  5.  public:
  6.   public foo(const int& f, const std::string& b): foo(f), bar(b){}
  7. };
  8.  
Mar 4 '08 #10
JohnSmith70
22 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template <typename avar, typename bvar, typename cvar>
  6. class AClass
  7. {
  8. public:
  9.   avar var1;
  10.   bvar var2;
  11.   cvar var3;
  12.   AClass (const avar& v1, const bvar& v2, const cvar& v3) 
  13.     : var1(v1), var2(v2), var3(v3) {}
  14.  
  15.   void forward(const AClass &a) 
  16.   {
  17.       //get current position and go plus 1
  18.   }
  19.   int back(const AClass &a) 
  20.   {
  21.       //get current position and go back 1
  22.      //display content of current position
  23.   }
  24. };
  25.  
  26. int main()
  27. {    
  28.  vector <AClass<int,int,int> > avector;
  29.  AClass<int, int, int> obj1(1,2,3);
  30.  avector.push_back(obj1);
  31. }
This doesn't work. Please help, I would really appreciate it.
Expand|Select|Wrap|Line Numbers
  1. display()
  2.   {
  3.            for( int i = 0; i < avector.size(); i++ )    
  4.             cout << avector[i] << " ";
  5.   }
John
Mar 4 '08 #11
Laharl
849 Recognized Expert Contributor
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.
Mar 4 '08 #12
JohnSmith70
22 New Member
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
Mar 4 '08 #13
Laharl
849 Recognized Expert Contributor
Go read this. It'll make more sense than any explanation I could give (and is far more thorough).
Mar 4 '08 #14
JohnSmith70
22 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template <typename avar, typename bvar, typename cvar>
  6. class AClass
  7. {
  8. public:
  9.   avar var1;
  10.   bvar var2;
  11.   cvar var3;
  12.   AClass (const avar& v1, const bvar& v2, const cvar& v3) 
  13.     : var1(v1), var2(v2), var3(v3) {}
  14.  
  15.   void forward(const AClass &a) 
  16.   {
  17.        //get current position and go plus 1
  18.        //avector.push_back();
  19.   }
  20.   int back(const AClass &a) 
  21.   {
  22.       //get current position and go back 1
  23.      //display content of current position
  24.     //avector.pop_back();
  25.   }
  26. };
  27.  
  28. int main()
  29. {    
  30.  vector <AClass<int,int,int> > avector;
  31.  AClass<int, int, int> obj1(1,2,3);
  32.  
  33.  avector.push_back(obj1); //how can I use he function forward(obj1) instead?
  34.   for( int i = 0; i < avector.size(); i++ ){
  35.         cout<<obj1.var1;
  36.         cout<<obj1.var2;
  37.         cout<<obj1.var3;
  38.         cout<<endl;
  39. }
  40.  AClass<int, int, int> obj2(4,5,6);
  41.  avector.pop_back(); //can I use the function back(obj1) instead?
  42.         cout<<obj2.var1;
  43.         cout<<obj2.var2;
  44.         cout<<obj2.var3;
  45.         cout<<endl;
  46. }
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
Mar 4 '08 #15
Laharl
849 Recognized Expert Contributor
I don't quite understand what forward() and back() are supposed to do, as using functions that have nothing to do with the vector shouldn't be trying to move through the vector. Unless you pass them a vector::iterator, which is silly since you can just use ++ and -- for that.

If this is a homework assignment and you're expected to use them, I'm not sure what to say. You could maintain an int (private, this time) that would hold your current position in the vector, and it could be incremented on a call to forward() and decremented on a call to back().
Mar 4 '08 #16

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

Similar topics

14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
7
by: Gil | last post by:
trying to use a template in a vector, in Visual Studio 6 on Windows 2000 #include <vector> using namespace std; template <typename T> class AnyValue { T val;
3
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
3
by: ravenous.wolves | last post by:
I'm trying to create a template function to convert from a vector of Es, to a managed array of Es. I want this to work with std::vector, or any other type that implements the call requirements of...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
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
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.