0

Why is VS giving me this error? I am quite new to OOP and Templates in C++ and I am trying to write simple Stack implementation using these techniques. I tried to google it but I did not find anything useful.

Code:

#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

Full build log - obviously there is a problem with 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 -> 
P. Bolfa
  • 99
  • 1
  • 7
  • I believe that is not the full error message, only the notes. If you could provide the part (line) where it actually says "error", that may be helpful – zaw Jun 20 '18 at 15:31
  • Possible duplicate of [Wrong use of std::copy?](https://stackoverflow.com/questions/27590239/wrong-use-of-stdcopy) – Joseph D. Jun 20 '18 at 15:49
  • 1
    Unrelated: `Stack::Stack(const Stack &obj)` doesn't allocate storage for `buffer`. – user4581301 Jun 20 '18 at 15:51
  • 1
    Not an error, by the way. This is Visual Studio warning you that using pointers in place of iterators implies that you are taking on some additional risk. Your math computing the end pointer MUST be correct or the program will misbehave. Your math looks good to me. – user4581301 Jun 20 '18 at 15:56
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_STACK`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Jun 20 '18 at 17:01
  • 2
    Reading compiler messages can be tedious, but the first rule is **begin at the beginning**. All those lines marked "note" are giving you more information about the issue, but it's the first line, marked "warning" (**not** "error" in this case), that tells you what the issue is. – Pete Becker Jun 20 '18 at 17:03

0 Answers0