为什么VS给我这个错误?我对C ++中的OOP和模板不是很熟悉,我正在尝试使用这些技术编写简单的Stack实现。我试图用Google搜索它,但没有发现任何有用的东西。
代码:
#ifndef _STACK
#define _STACK
template <class T>
class Stack {
public:
Stack();
Stack(const int &num);
Stack(const Stack<T> &obj);
Stack& operator=(const Stack<T> &obj);
~Stack();
private:
T * buffer;
unsigned int actualSize;
unsigned int maxSize;
};
template <class T>
Stack<T>::Stack() : maxSize(5), actualSize(0), buffer(new T[5]) {};
template <class T>
Stack<T>::Stack(const int &num) : maxSize(num), actualSize(num), buffer(new T[num]) {};
template <class T>
Stack<T>::Stack(const Stack<T> &obj) {
this->actualSize = obj.actualSize;
this->maxSize = obj.maxSize;
std::copy(obj.buffer, obj.buffer + obj.actualSize, this->buffer);
}
template <class T>
Stack<T>& Stack<T>::operator=(const Stack<T> &obj) {
T * temp = new T[obj.actualSize];
std::copy(obj.buffer, obj.buffer + obj.actualSize, temp);
this->actualSize = obj.actualSize;
this->maxSize = obj.maxSize;
delete[] buffer;
buffer = temp;
return *this;
}
template <class T>
Stack<T>::~Stack() {
delete[] buffer;
}
#endif
完整的构建日志-显然std :: copy有问题:
1>------ Build started: Project: Stack, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2483): warning C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2483): note: see declaration of 'std::copy::_Unchecked_iterators::_Deprecate'
1>c:\users\peter\documents\pb071hw\circle\stack\stack.h(34): note: see reference to function template instantiation '_OutIt std::copy<T*,T*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=int *,
1> T=int,
1> _InIt=int *
1> ]
1>c:\users\peter\documents\pb071hw\circle\stack\stack.h(32): note: while compiling class template member function 'Stack<int> &Stack<int>::operator =(const Stack<int> &)'
1>c:\users\peter\documents\pb071hw\circle\stack\main.cpp(13): note: see reference to function template instantiation 'Stack<int> &Stack<int>::operator =(const Stack<int> &)' being compiled
1>c:\users\peter\documents\pb071hw\circle\stack\main.cpp(10): note: see reference to class template instantiation 'Stack<int>' being compiled
1>Stack.vcxproj ->