std::copyを使用するとコンパイルエラーになります。
何が原因なのか教えてください。
(書籍「ロベールのC++入門講座」p302のコードです)
コンパイラ: Visual studio community 2015
OS: Windows7 64 bit
エラーメッセージ
C4996 'std::_Copy_impl': Function call 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'
C++
1// CIntArray.h 2#ifndef C_INT_ARRAY_H 3#define C_INT_ARRAY_H 4 5class CIntArray { 6private: 7 int* array; 8 int arraySize; 9 10public: 11 CIntArray(int arraySize); 12 CIntArray(const CIntArray& intArray); 13 ~CIntArray(); 14 15 int Get(int index); 16 void Set(int index, int data); 17 int Size(); 18 19private: 20 void CheckIndex(int index); 21}; 22 23#endif
C++
1// CIntArray.cpp 2#include "CIntArray.h" 3#include <iostream> 4#include <cstdlib> 5#include <algorithm> 6 7CIntArray::CIntArray(int arraySize) { 8 array = new int[arraySize]; 9 this->arraySize = arraySize; 10 std::cout << "constructor: " << arraySize << std::endl; 11} 12 13CIntArray::CIntArray(const CIntArray& intArray) { 14 array = new int[intArray.arraySize]; 15 arraySize = intArray.arraySize; 16 std::copy(intArray.array, intArray.array + intArray.arraySize, array); 17 std::cout << "copy constructor" << std::endl; 18} 19 20CIntArray::~CIntArray() { 21 std::cout << "destructor: " << arraySize << std::endl; 22 delete[] array; 23} 24 25int CIntArray::Get(int index) { 26 CheckIndex(index); 27 return array[index]; 28} 29 30void CIntArray::Set(int index, int data) { 31 CheckIndex(index); 32 array[index] = data; 33} 34 35int CIntArray::Size() { 36 return this->arraySize; 37} 38 39void CIntArray::CheckIndex(int index) { 40 if (0 <= index && index < arraySize) { 41 // no process 42 } else { 43 std::cout << "インデックスが不正:" << index << std::endl; 44 exit(EXIT_FAILURE); 45 } 46}
C++
1// main.cpp 2#include "CIntArray.h" 3#include <iostream> 4 5void Show(CIntArray array) { 6 for (int i = 0; i < array.Size(); i++) { 7 std::cout << array.Get(i) << std::endl; 8 } 9} 10 11int main() { 12 CIntArray array0to9(10); 13 14 for (int i = 0; i < array0to9.Size(); i++) { 15 array0to9.Set(i, i); 16 } 17 18 Show(array0to9); 19}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/19 15:30
2016/01/19 15:41
2016/01/19 16:17
2016/01/20 10:54