Я знаю, что указатели - это то, что обсуждается много. Я провел много исследований, чтобы попытаться решить эту проблему, однако все ведет меня в тупик.
У меня есть задание, которое просит меня создать класс, который записывает один результат теста. Если тестовый счет уже записан и новый балл выше, переопределите его. Если он был записан и новый балл ниже, ничего не делайте. Если он не был записан, запишите его.
Вот что я имею до сих пор:
// CIS 235 exercise 7
#include <iostream>
using namespace::std;
// declare a class for recording a test score
// the data will be pointer to an integer, rather than an integer
//
// - this exercise is designed to show how to work with pointer memory
// - of course, we would NOT normally use a pointer for just an integer
// - to illustrate the concepts, but keep the implementation simple,
// integer data was used. The general case would be object data,
// not integer data
class testScore
{
public:
// declare a default constructor - the pointer should be set to NULL
testScore();
// declare a function that returns a bool, indicating if the test has been taken
bool hasTestTaken();
// declare a function to record the test score, the parameter will be an integer
// use the following rules
// - if no test has been taken, allocate memory and record the score
// - if a test has been taken and the parameter is less than or equal to
// the score, do nothing
// - if the test has been taken and the parameter is higher than the score,
// - release the old memory
// - allocate new memory
// - record the score
void recordScore(int *myScore);
// declare a function to print the score to an ostream parameter
// if the test has not been taken, send an appropriate message to the ostream
// otherwise print the score
void printScore(ostream &out);
// declare the destructor
// be CAREFUL, you will need an if statement in your destructor
~testScore();
private:
// declare the data needed to implement the class
bool testTaken;
int *score;
};
// write the 5 member functions
testScore::testScore() : score(NULL)
{
// declare a default constructor - the pointer should be set to NULL
}
bool testScore::hasTestTaken()
{
// declare a function that returns a bool, indicating if the test has been taken
return testTaken;
}
void testScore::recordScore(int *myScore)
{
if(testTaken == false)
{
testTaken = true;
*score = *myScore;
}
else if(testTaken == true && *myScore > *score)
{
score = NULL;
delete score;
score = new int;
*score = *myScore;
}
}
void testScore::printScore(ostream& out)
{
// declare a function to print the score to an ostream parameter
// if the test has not been taken, send an appropriate message to the ostream
// otherwise print the score
if(testTaken)
{
out << *score << endl;
}
else
out << "The test has not been taken!" << endl;
}
testScore::~testScore()
{
// declare the destructor
// be CAREFUL, you will need an if statement in your destructor
if(score != NULL)
{
score = NULL;
delete score;
}
else
delete score;
}
// test the class member functions
// - declare an object, but do NOT record a score for the object
// - declare a second object and record the scores of 83, 78, 92
// use appropriate member print functions to verify your code
//int abc = 83;
int abc = 0;
int main()
{
// int abc = 0;
// int * score2;
// myTestScore = new int;
// *myTestScore = 83;
testScore firstScore;
firstScore.printScore(cout);
testScore secondScore;
// secondScore.recordScore(&abc);
secondScore.recordScore(&abc);
// secondScore.printScore(cout);
// *myTestScore = 78;
// secondScore.recordScore(myTestScore);
// secondScore.printScore(cout);
// *myTestScore = 92;
// secondScore.recordScore(myTestScore);
// secondScore.printScore(cout);
system("PAUSE");
return 0;
}
Указатели - это что-то новое для меня... Я посмотрел на них, посмотрел на них и посмотрел на них, но, похоже, ВСЕГДА их неправильно.
Зная это, я знаю, что моя функция recordScore, вероятно, делает что-то очень неправильное, но я не знаю, что.
Моя главная проблема прямо сейчас в том, что firstScore работает отлично (yay! Я получил что-то правильно... может быть), однако secondScore не будет записывать счет. Я пробовал несколько разных способов.
Я положил int abc = 0; выше int main()
Я положил int abc = 0; внутри int main(), но прежде всего
Это также сбой, если my int main() выглядит так:
int main()
{
int abc = 0;
testScore firstScore;
firstScore.printScore(cout);
system("PAUSE");
return 0;
}
И я понятия не имею, почему TT
Я также пробовал:
декларирование
int *myTestScore;
myTestScore = new int;
*myTestScore = 83;
внутри main(), но прежде всего, и передавая myTestScore в recordScore через:
&myTestScore
Ошибка компиляции: нет соответствующей функции для вызова "testScore :: recordScore (int **); на строке secondScore.recordScore.
*myTestScore
Ошибка компиляции: неверное преобразование из 'int' в 'int *' в строке secondScore.recordScore.
myTestScore
Ошибка компиляции, сбой при запуске до того, как что-то выйдет на консоль
Я пробовал объявить:
int *myTestScore = 83;
внутри int main() перед чем-либо еще Ошибка компиляции: неверное преобразование из 'int' в 'int *' в строке int * myTestScore = 83.
Я также пробовал различные методы смены recordScore на использование & и * и ни с одной, ни с другой комбинацией обоих.
У меня теперь нет идей, чтобы попробовать, и даже после исследования я ничего не могу придумать. Я попытался спросить моего профессора (в течение недели сейчас, он на онлайн-курсе), позвонив ей, отправив ей по электронной почте, но она не ответила ни на один из вопросов, которые у меня есть, или даже на просьбу о встрече.
Я чувствую, что есть что-то простое, что я не понимаю здесь, и я очень ценю любую помощь, которую кто-то может дать мне понять эту проблему.
Большое вам спасибо за ваше время.
Изменения:
testScore::testScore() : score(NULL), testTaken(false) // didnt change because instructor instructions, but did move testTaken up cause that is where it should be
{
// declare a default constructor - the pointer should be set to NULL
}
void testScore::recordScore(int myScore)
{
if(testTaken == false)
{
testTaken = true;
score = &myScore;
cout << *score << endl; //this prints correctly, 0
}
else if(testTaken == true && myScore > *score)
{
//removed the score = NULL to avoid a memory leak (I think this is correct now?)
delete score;
score = new int;
score = &myScore;
}
}
void testScore::printScore(ostream& out)//no changes, just easier to access to you dont have to keep scrolling up
{
// declare a function to print the score to an ostream parameter
// if the test has not been taken, send an appropriate message to the ostream
// otherwise print the score
if(testTaken)
{
out << *score << endl; //outputs incorrect 4469696
}
else
out << "The test has not been taken!" << endl;
}
int main()
{
int abc = 0;
testScore firstScore;
firstScore.printScore(cout);
testScore secondScore;
secondScore.recordScore(abc);
secondScore.printScore(cout);
system("PAUSE");
return 0;
}
Выходы: этот тест не был выполнен! 0 4469696 Нажмите любую клавишу, чтобы продолжить...
Конечный рабочий продукт:
// CIS 235 exercise 7
#include <iostream>
using namespace::std;
// declare a class for recording a test score
// the data will be pointer to an integer, rather than an integer
//
// - this exercise is designed to show how to work with pointer memory
// - of course, we would NOT normally use a pointer for just an integer
// - to illustrate the concepts, but keep the implementation simple,
// integer data was used. The general case would be object data,
// not integer data
class testScore
{
public:
// declare a default constructor - the pointer should be set to NULL
testScore();
// declare a function that returns a bool, indicating if the test has been taken
bool hasTestTaken();
// declare a function to record the test score, the parameter will be an integer
// use the following rules
// - if no test has been taken, allocate memory and record the score
// - if a test has been taken and the parameter is less than or equal to
// the score, do nothing
// - if the test has been taken and the parameter is higher than the score,
// - release the old memory
// - allocate new memory
// - record the score
void recordScore(int * myScore);
// declare a function to print the score to an ostream parameter
// if the test has not been taken, send an appropriate message to the ostream
// otherwise print the score
void printScore(ostream &out);
// declare the destructor
// be CAREFUL, you will need an if statement in your destructor
~testScore();
private:
// declare the data needed to implement the class
bool testTaken;
int *score;
};
// write the 5 member functions
testScore::testScore() : score(NULL), testTaken(false)
{
// declare a default constructor - the pointer should be set to NULL
}
bool testScore::hasTestTaken()
{
// declare a function that returns a bool, indicating if the test has been taken
return testTaken;
}
void testScore::recordScore(int * myScore)
{
if(testTaken == false)
{
score = new int;
testTaken = true;
*score = *myScore;
}
else if(testTaken == true && *myScore > *score)
{
delete score;
score = new int;
*score = *myScore;
}
}
void testScore::printScore(ostream& out)
{
// declare a function to print the score to an ostream parameter
// if the test has not been taken, send an appropriate message to the ostream
// otherwise print the score
if(testTaken)
{
out << *score << endl;
}
else
out << "The test has not been taken!" << endl;
}
testScore::~testScore()
{
// declare the destructor
// be CAREFUL, you will need an if statement in your destructor
if(score != NULL)
{
delete score;
}
}
// test the class member functions
// - declare an object, but do NOT record a score for the object
// - declare a second object and record the scores of 83, 78, 92
// use appropriate member print functions to verify your code
int main()
{
int abc = 83;
testScore firstScore;
firstScore.printScore(cout);
testScore secondScore;
secondScore.recordScore(&abc);
secondScore.printScore(cout);
abc = 78;
secondScore.recordScore(&abc);
secondScore.printScore(cout);
abc = 92;
secondScore.recordScore(&abc);
secondScore.printScore(cout);
system("PAUSE");
return 0;
}
Большое вам спасибо, на самом деле я немного чему-то научился и несколько новых терминов :)
Основная проблема заключается в том, что в конструкторе по умолчанию вы назначаете NULL
для оценки, поэтому указатель укажет на недопустимую память. Итак, когда вы вызываете recordStore, когда программа приходит к этой инструкции:
*score = *myScore;
Это приводит к ошибке сегментации, которая возникает при попытке перезаписать часть памяти, которую ваша программа не использует.
Программа не будет терпеть крах в printScore, потому что чтение недопустимого указателя не является ошибкой, но оно будет читать данные мусора.
EDIT: согласно вашему назначению, указатель должен быть выделен в recordStore, если тест не был выполнен, поэтому в recordStore измените эту часть:
if(testTaken == false)
{
testTaken = true;
*score = *myScore;
}
к этому:
if(testTaken == false)
{
score = new int;
testTaken = true;
*score = *myScore;
}
Также, когда вы делаете часть delete
, вы назначаете сначала указатель на NULL, а затем удаляете его; поэтому программа попытается удалить указатель NULL
(это не приведет к ошибке), а память, используемая для score
, не будет выпущена, что приведет к утечке памяти.