gpt4 book ai didi

C++从main()通过类函数传递指针

转载 作者:行者123 更新时间:2023-11-28 07:21:49 26 4
gpt4 key购买 nike

我知道指针是一个被广泛讨论的话题。我做了很多研究来尝试解决这个问题,但是一切都让我走到了死胡同。

我的作业要求我创建一个记录单个测试分数的类。如果已经记录了测试分数并且新分数更高,则覆盖它。如果它已被记录并且新分数较低,则什么都不做。如果没有记录,请记录下来。

这是我目前所拥有的:

//  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;
}

Pointers 对我来说是相当新鲜的东西……我查过,查过,查过,但我似乎总是弄错了。

知道这一点后,我知道我的 recordScore 函数可能做错了什么,但我不知道是什么。

我现在的主要问题是 firstScore 运行良好(耶!我做对了......也许吧)但是,secondScore 不会记录分数。我尝试了几种不同的方法。

  1. 我把 int abc = 0;在 int main() 之上

    • 调用 recordScore 时编译并运行良好
    • 在我调用 printScore 时编译并崩溃
      • 输出显示:此测试已完成!按任意键继续...(崩溃)
  2. 我把 int abc = 0;在 int main() 内部但在其他任何事情之前

    • 在任何内容输出到控制台之前调用 recordScore 时崩溃

如果我的 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 更改为使用 & 和 * 以及两者都不使用以及两者的不同组合。

我现在对尝试的事情没有想法,甚至在研究之后我也想不出任何东西。我试过问我的教授(一周以来,在线类(class)),给她打电话,给她发电子邮件,但她没有回答我提出的任何问题,甚至没有要求会面。

我觉得这里有一些简单的事情我没有掌握,我真的很感谢任何人能给我的帮助来解决这个问题。

非常感谢您的宝贵时间。


变化:

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;
}

输出:此测试未参加!04469696按任意键继续...


最终工作产品:

//  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 分配给 score,因此指针将指向无效内存。所以,当你调用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的内存没有释放,造成内存泄漏

关于C++从main()通过类函数传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com