嘿伙计们我有这个代码:
sIP
我尝试解决了很多错误,但是没有带来任何好处!我创建了一个与libary一样的功能,现在我正在创建一个类。我需要帮助!这是错误:
#include<iostream>
#include<string>
#include<list>
#include<vector>
template<class T>
class ListLib {
public:
ListLib(std::list<T>& listItem) {
*listT = listItem;
}
std::vector<T> convertToVector() {
std::vector<T> result;
std::list<T>::iterator itList;
for (itList = *listT->begin(); itList != *listT->end(); itList++)
result->(*itList);
std::vector<T>::iterator itVec;
for (itVec = result.begin(); itVec != result.end())
std::cout << *itVec << std::endl;
}
private:
std::list<T> *listT;
};
int main() {
std::list<std::string> sampleList;
std::vector<std::string> sampleVector;
sampleList.push_back("3");
sampleList.push_back("4");
sampleList.push_back("5");
sampleList.push_back("6");
ListLib<std::string> listLib(sampleList);
std::cout << "Hello World" << std::endl;
}
答案 0 :(得分:1)
您缺少许多分号,并且在所有itterators之前还需要关键字typename
..这可以解决您遇到的所有问题:
#include<iostream>
#include<string>
#include<list>
#include<vector>
template<class T>
class ListLib {
public:
ListLib(std::list<T>& listItem) {
*listT = listItem;
}
std::vector<T> convertToVector() {
std::vector<T> result;
typename std::list<T>::iterator itList;
for (itList = *listT->begin(); itList != *listT->end(); itList++)
result.push_back(*itList);
typename std::vector<T>::iterator itVec;
for (itVec = result.begin(); itVec != result.end(); itVec++)
std::cout << *itVec << std::endl;
}
private:
std::list<T> *listT;
};
int main() {
std::list<std::string> sampleList;
std::vector<std::string> sampleVector;
sampleList.push_back("3");
sampleList.push_back("4");
sampleList.push_back("5");
sampleList.push_back("6");
ListLib<std::string> listLib(sampleList);
std::cout << "Hello World" << std::endl;
}
/*template<class T>
void convertListToVector(std::list<T> *listItem, std::vector<T> *vectorConvert) {
std::list<T>::iterator it;
for (it = *listItem->begin(); it != *listItem->end(); it++)
*vectorConvert->push_back(*it);
}*/
工作 "C++ Fiddle
"