생성자에서 메모리를 할당 한 다음 소멸자에서 []을 (를) 삭제하지만, 나는 _Block_Type_Is_Valid (pHead->nBlockUse)" Error
이됩니다. 나는 세 가지의 규칙은을 찾아왔다. 내 프로그램에서 나는 단지 하나의 인스턴스를 가지고 있고 Copy Constructor 나 Copy Assignment Operator도 사용하지 않기 때문에 명시 적으로 선언 된 소멸자가 필요하다고 생각했다. 생성자에서 char 배열의 값을 복사하고 무엇이 잘못 되었는 지 알 수 없습니다. 다음은 프로그램 코드의 간단한 목록입니다. 헤더 :삭제자 내에서 []을 (를) 삭제하여 생성자 내에 할당하십시오.
class CTest
{
private:
char *TestTable;
int TestTableLength;
std::chrono::steady_clock::time_point StartPoint;
std::chrono::steady_clock::time_point EndPoint;
std::chrono::steady_clock::time_point CheckPoint;
std::chrono::system_clock::duration d;
public:
CTest(char *SignTable);
~CTest();
void NewCombination();
bool AskToPlay();
bool AskForNewCombination();
void Play();
};
출처 :
CTest::CTest(char *SignTable)
{
int SignTableLength = 0;
for (int i = 0; *(SignTable + i) != '\0'; i++)
{
SignTableLength++;
}
TestTableLength = SignTableLength ;
TestTable = new char[TestTableLength];
for (int i = 0; *(SignTable + i) != '\0'; i++)
{
*(TestTable + i) = *(SignTable + i);
}
}
CTest::~CTest()
{
delete[] TestTable;
}
void CTest::NewCombination()
{
int tmpInt;
char tmpChar;
if (TestTable != NULL)
{
for (int i = 0; i < TestTableLength - 1; i++)
{
tmpInt = rand() % (TestTableLength - i);
tmpChar = TestTable[TestTableLength - 1 - i];
TestTable[TestTableLength - 1 - i] = TestTable[tmpInt];
TestTable[tmpInt] = tmpChar;
}
}
}
bool CTest::AskToPlay()
{
char play;
std::cout << "Do you want to play again ?" << std::endl << "If yes press 'y' else press something else" << std::endl;
std::cin >> play;
if (play == 'y') return true;
else return false;
};
bool CTest::AskForNewCombination()
{
char newcom;
std::cout << "Do you want me to create new combination?" << std::endl << "If yes press 'y' else press something else" << std::endl;
std::cin >> newcom;
if (newcom == 'y') return true;
else return false;
};
void CTest::Play()
{
StartPoint = std::chrono::steady_clock::now();
CheckPoint = std::chrono::steady_clock::now();
std::cout << "3\t";
d = CheckPoint - StartPoint;
while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 1000)
{
CheckPoint = std::chrono::steady_clock::now();
d = CheckPoint - StartPoint;
}
std::cout << "2\t";
while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 2000)
{
CheckPoint = std::chrono::steady_clock::now();
d = CheckPoint - StartPoint;
}
std::cout << "1" << std::endl;
while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 3000)
{
CheckPoint = std::chrono::steady_clock::now();
d = CheckPoint - StartPoint;
}
std::cout << "START!" << std::endl;
StartPoint = std::chrono::steady_clock::now();
for (int i = 0; i < (TestTableLength) ; i++)
{
std::cout << *(TestTable + i) << " ";
}
};
홈페이지 :
char Signs[] = { '1', '2', '3', '4', '5', '6', 'q', 'w', 'e', 'r', 'f', 'g' };
CTest Test(Signs);
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
while (Test.AskToPlay())
{
if (Test.AskForNewCombination()) Test.NewCombination();
Test.Play();
}
Test.~CTest();
return 0;
}
'std :: string'을 사용하십시오. – chris
아마도 복사 생성자를 인식하지 않고 사용하고 있습니다. 이 클래스를 사용하여 오류를 일으키는 예제를 제공해야합니다. 객체가 복사 불가능한 경우 객체에 코드를 구현해야합니다. [다음과 같은 메커니즘이 있습니다] (http://stackoverflow.com/questions/2173746/how-do-i-make-this-c-object-non-copyable). –
이것은 문제가 아니지만 'SignTable [i]'대신'* (SignTable + i)'를 사용하는 이유가 있습니까? – wolfPack88