Currently, I have this:
void ClassA::aFunction(QVector<ClassB *> items) {
//stuff
}
I would like to turn the parameter for ClassA::aFunction into a QVector with templates:
template <class T *> class QVector;
void ClassA::aFunction(QVector<T *> items) {
//stuff
}
This is giving me syntax errors, so what is the correct way to do this?
Edit:
I have this now:
classA.h:
template <class T>
class ClassA {
public:
void myFunction(ClassB *);
}
classA.cpp:
template <class T>
void ClassA<T>::myFunction(ClassB * b) {
QVector<T *> var = b->myBFunction();
}
classB.h:
template <class T>
class ClassB {
public:
void myBFunction();
private:
QVector<T *> myVar;
}
classB.cpp:
template <class T>
QVector<T *> ClassB<T>::myBFunction() {
return this->myVar;
}
I have a syntax error “member declaration not found” with ClassA::myFunction. I also have an error “method ‘myBFunction’ could not be resolved” in ClassA::myFunction. How do I fix these two errors?
Edit2
Figured this out:
classA.h:
class ClassA {
public:
template <class T> void myFunction(ClassB *);
}
classA.cpp:
template <class T>
void ClassA::myFunction(ClassB * b) {
QVector<T *> var = b->myBFunction<T>();
}
classB.h:
class ClassB {
public:
template <class T> void myBFunction();
private:
template <class T> QVector<T *> myVar;
}
classB.cpp:
template <class T>
QVector<T *> ClassB::myBFunction() {
return this->myVar;
}
Almost right:
You’ll have to declare that in the same way inside the class definition:
By the way, why are you passing a
QVectorby value?