以下のコピーコンストラクタのサンプルコードをコンパイルすると、
'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'
という内容のエラーが出てきます。
なぜエラーが出るのでしょうか?
copy関数の部分が何かおかしいのでしょうか…
言語はVC++です。
IntArray.cpp
VC++
1#include "IntArray.h" 2#include <iostream> 3#include <algorithm> //copy 関数に必要 4#include <cstdlib> //exit 関数に必要 5using namespace std; 6 7 8 9//コピーコンストラクタ 10IntArray::IntArray(const IntArray& other){ 11 m_array = new int[other.m_size]; 12 m_size = other.m_size; 13 14 copy(other.m_array, other.m_array + m_size, m_array); 15 16 17 cout << "コピーコンストラクタが呼ばれました。" << endl; 18} 19 20 21//コンストラクタ 22IntArray::IntArray(int size){ 23 m_array = new int[size]; 24 m_size = size; 25 26 cout << "コンストラクタが呼ばれました。" << "要素数は" << m_size << "です。" << endl; 27} 28 29 30//デストラクタ 31IntArray::~IntArray(){ 32 delete[] m_array; 33 34 cout << "デストラクタが呼ばれました。" << "要素数は" << m_size << "でした。" << endl; 35} 36 37 38//メンバへのアクセス関数 39int IntArray::Get(int i){ 40 void CheckIndex(int i); //インデックスのチェック 41 return m_array[i]; 42} 43 44 45void IntArray::Set(int i, int value){ 46 void CheckIndex(int i); //インデックスのチェック 47 m_array[i] = value; 48} 49 50 51//インデックスのチェック 52void IntArray::CheckIndex(int i){ 53 if (0 <= i && i < m_size){ 54 //インデックスは正常です 55 } 56 else{ 57 cout << "インデックスが不正です!" << endl << "値 : " << i << endl; 58 exit(EXIT_FAILURE); 59 } 60} 61 62 63int IntArray::Size(){ 64 return m_size; 65}
Copy.cpp
VC++
1#include "IntArray.h" 2#include <iostream> 3using namespace std; 4 5 6 7void Viss(int num){ 8 cout << "Viss : No." << num << endl; 9} 10 11 12void Show(IntArray array){ 13 14 Viss(2); 15 16 for (int i = 0; i < array.Size(); i++){ 17 cout << array.Get(i) << ' '; 18 } 19 cout << endl; 20 21 Viss(3); 22} 23 24 25int main(){ 26 IntArray array0to9(10); 27 28 for (int i = 0; i < array0to9.Size(); i++){ 29 array0to9.Set(i, i); 30 } 31 32 Viss(1); 33 Show(array0to9); 34 Viss(4); 35} 36
IntArray.h
VC++
1 2class IntArray{ 3public: 4 IntArray(const IntArray& other); //コピーコンストラクタ 5 IntArray(int size); //コンストラクタ 6 ~IntArray(); //デストラクタ 7 8 int Get(int i); //メンバへのアクセス関数(取得) 9 void Set(int i, int value); //メンバへのアクセス関数(変更) 10 void CheckIndex(int i); //インデックスのチェック 11 12 int Size(); //配列のサイズを返す 13 14 15private: 16 int* m_array; //動的配列 17 int m_size; //配列の要素数 18 19}; 20
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/17 15:46