B
bb
Hi,
On compiling the following simple template backed by 'std::vector', get
the following compile time error:
----- Compilation error ----------
tc2.h: In member function 'void MyArray<T>::display()':
tc2.h:36: error: expected `;' before 'ivi'
tc2.h:37: error: 'ivi' was not declared in this scope
tc2.h: In member function 'void MyArray<T>::display() [with T =
------ Code --------------------------
template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;
};
template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();
}
template<typename T>
MyArray<T>::~MyArray() {
delete v;
}
template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);
}
template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}
int main(int argc, char** argv) {
MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");
ma1.display();
}
------- Code ends -----------------------------------
Any comments/help please?
Thanks.
On compiling the following simple template backed by 'std::vector', get
the following compile time error:
----- Compilation error ----------
tc2.h: In member function 'void MyArray<T>::display()':
tc2.h:36: error: expected `;' before 'ivi'
tc2.h:37: error: 'ivi' was not declared in this scope
tc2.h: In member function 'void MyArray<T>::display() [with T =
tc2.h:49: instantiated from here
tc2.h:36: error: dependent-name 'std::vector said:::iterator' is parsed as a non-type, but instantiation yields a type
tc2.h:36: note: say 'typename std::vector said:::iterator' if a type is meant
------ Code --------------------------
template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;
};
template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();
}
template<typename T>
MyArray<T>::~MyArray() {
delete v;
}
template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);
}
template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}
int main(int argc, char** argv) {
MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");
ma1.display();
}
------- Code ends -----------------------------------
Any comments/help please?
Thanks.