我知道指针是经常讨论的东西。我已经做了很多研究试图解决这个问题,但是一切都让我陷入了死胡同。
我的作业要求我创建一个记录单个考试成绩的课程。如果已经记录了测试分数并且新分数更高,则覆盖它。如果已记录并且新分数较低,则不执行任何操作。如果尚未录制,请进行录制。
这是我到目前为止所做的:
// 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运行良好(是的!我得到了一些正确的......也许)但是,secondScore不会记录得分。我尝试了几种不同的方式。
我把int abc = 0;在int main()
之上我把int abc = 0;在int main()中,但在其他任何东西之前
如果我的int main()看起来像这样,这也会崩溃:
int main()
{
int abc = 0;
testScore firstScore;
firstScore.printScore(cout);
system("PAUSE");
return 0;
}
我不知道为什么T.T
我也尝试过:
声明
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更改为使用&amp;和's',并且两者都没有变化。
我现在已经没有尝试过的东西,即使在研究之后我也无法想出任何东西。我试着问我的教授(现在已经有一个星期了,这是在线课程),打电话给她,给她发电子邮件,但她没有回答我的任何问题,甚至没有回复会议的要求。
我觉得有一些简单的事情我不会在这里抓住,我真的很感激任何人都可以帮助我解决这个问题。
非常感谢您的时间。
的变化:
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;
}
非常感谢你,我实际上从中学到了很多东西,还有一些新的术语:)
答案 0 :(得分:1)
主要问题是在默认构造函数中指定NULL
得分,因此指针将指向无效内存。所以,当你调用recordStore时,当程序来到这个指令时:
*score = *myScore;
导致分段错误,当您尝试覆盖程序未使用的部分内存时会发生错误。
程序不会在printScore中崩溃,因为读取无效指针不是错误,但它会读取垃圾数据。
编辑:根据你的作业,如果没有进行测试,必须在recordStore中分配指针,所以在recordStore中,改变这一部分:
if(testTaken == false)
{
testTaken = true;
*score = *myScore;
}
到此:
if(testTaken == false)
{
score = new int;
testTaken = true;
*score = *myScore;
}
当你执行delete
部分时,首先将指针指定为NULL,然后将其删除;因此程序将尝试删除NULL
指针(这不会导致错误),并且score
使用的内存不会释放,导致内存泄漏。