错误
stack.cc:53:28: error: no matching function for call to ‘Stack<std::basic_string<char> >::push(std::string)’
stack.cc:53:28: note: candidate is:
stack.cc:32:11: note: Stack<T>& Stack<T>::push(T&) [with T = std::basic_string<char>]
堆栈.cc
#include<iostream>
template <typename T>
class Stack {
private:
T* array_;
int length_;
T* last_;
void expandArray();
public:
Stack(int length = 8) {
array_ = new T[length];
length_ = length;
last_ = array_;
}
Stack<T>& push(T&);
T pop();
};
template<typename T>
void Stack<T>::expandArray() {
T* array_temp = new T[length_ << 1];
memcpy(array_temp, array_, length_);
std::swap(array_, array_temp);
delete[] array_temp;
length_ <<= 1;
}
template<typename T>
Stack<T>& Stack<T>::push(T& data) {
if (last_ == (array_ + length_ - 1)) {
expandArray();
}
last_[0] = data;
last_++;
return *this;
}
template<typename T>
T Stack<T>::pop() {
if(array_ != last_) {
T temp = last_[0];
last_--;
return temp;
}
return NULL;
}
int main() {
Stack<std::string> s;
s.push(std::string("a"))
.push(std::string("b"))
.push(std::string("c"))
.push(std::string("d"));
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
}
std::string
想了解为什么从to发生转换std::basic_string<char>
?
请随时评论代码质量。