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 -
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
15 1914
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. -
vector <AClass<int,string, float> > avector;
-
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.
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: - 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
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.
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: - 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
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.
Thanks for your response dear friend. Here is my 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);
-
}
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
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.
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.
- #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
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. -
class foo{
-
private:
-
int foo;
-
std::string bar;
-
public:
-
public foo(const int& f, const std::string& b): foo(f), bar(b){}
-
};
-
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: - #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. - display()
-
{
-
for( int i = 0; i < avector.size(); i++ )
-
cout << avector[i] << " ";
-
}
John
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.
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
Laharl 849
Recognized Expert Contributor
Go read this. It'll make more sense than any explanation I could give (and is far more thorough).
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. - #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
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().
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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;
|
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
{
};
|
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;
|
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...
| |
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...
|
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;...
|
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...
|
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....
|
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
|
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...
| |
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...
|
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...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |