简体   繁体   English

程序停止工作,我确定指针错误

[英]Program stops working, pointer error i'm sure

I have this program, and I'm still getting used to C++ pointers, so It's probably an issue with that. 我有这个程序,而且我仍然习惯于C ++指针,所以这可能是一个问题。 But I am having the program crash when the getStructData() function is called. 但是,当调用getStructData()函数时,程序崩溃。 I've probably messed up something to do with the pointer to the struct that i've used, but I'm really not sure at this point. 我可能已经弄乱了与我所使用的结构的指针有关的内容,但是我目前还不确定。 Any tips or help are appreciated. 任何提示或帮助表示赞赏。 Thanks, and before people start the mad downvoting, this isn't a homework assignment from my school, I'm just going over other schools homework to practice during the christmas break. 谢谢,在人们开始疯狂地投反对票之前,这不是我学校的作业,我只是在圣诞节假期里翻阅其他学校的作业来练习。

Prog1Struct.h Prog1Struct.h

#ifndef INCLUDED_PROG1STRUCT
#define INCLUDED_PROG1STRUCT

struct Prog1Struct
{

int m_iVal;
double m_dArray[5];
char m_sLine[80];

};

#endif

Prog1Class.h

#ifndef PROG1CLASS
#define PROG1CLASS
#include "Prog1Struct.h"



class Prog1Class
{

private:
Prog1Struct myStruct[5];



public:

/*Prog1Class();
~Prog1Class();*/
void setStructData();
void getStructData(int structureArrayIndex, struct Prog1Struct *info);
void printStruct(int indexPriv);
void printData(); 

};

#endif

Prog1Class.cpp Prog1Class.cpp

#ifndef INCLUDED_PROG1CLASS
#define INCLUDED_PROG1CLASS

#include <iostream>
#include <string>
#include "Prog1Class.h"
#include "Prog1Struct.h"
#include <time.h>

using namespace std;




void Prog1Class::setStructData()
{
for (int i = 0; i<5; i++)
{
cout << "Enter an integer: ";
cin >> myStruct[i].m_iVal;
for (int j = 0; j < 5; j++)
{
    cout << endl << "Enter a double: ";
    cin >> myStruct[i].m_dArray[j];
}
cout << endl << "Enter a string: ";
cin.ignore(256, '\n');
cin.getline(myStruct[i].m_sLine, 80, '\n');
cout << endl;
}
}

//takes in index for array, and pointer to a struct of the type in Prog1Struct.h.  Copies     all data from the private struct at the given index into the struct of the pointer     argument.
void Prog1Class::getStructData(int structureArrayIndex, struct Prog1Struct *info)
{

*info = myStruct[structureArrayIndex];
cout << "Printing *info from getStructData function" << endl;
cout << info;
}


void Prog1Class::printStruct(int indexPriv)
{
cout << myStruct[indexPriv].m_iVal << " ";
for (int k = 0; k < 5; k++)
{
cout << myStruct[indexPriv].m_dArray[k] << " ";
}
cout << myStruct[indexPriv].m_sLine << " ";
}


int main(void)
{
Prog1Class c;
Prog1Struct *emptyStruct = '\0';
cout << "setStructData called:" << endl;
c.setStructData();
cout << "getStructData called:" << endl;
//error comes here, at getStructData.  
c.getStructData(2, emptyStruct);
cout << "printStruct called:" << endl;
c.printStruct(2);


cin.get();
}



#endif

You are trying to assign a value to a null pointer. 您正在尝试为空指针分配一个值。

Prog1Struct *emptyStruct = '\0';//set emptyStruct to 0 (This means it points at address 0, not holding a 0 value)
cout << "setStructData called:" << endl;
c.setStructData();
cout << "getStructData called:" << endl;
//error comes here, at getStructData.  
c.getStructData(2, emptyStruct);//emptyStruct is still = 0

So in the function, info = 0. 因此在函数中,info = 0。

void Prog1Class::getStructData(int structureArrayIndex, struct Prog1Struct *info)
{

*info = myStruct[structureArrayIndex];//This line is trying to write to a section of memory you don't have access to (address 0)

I think you want this instead. 认为您想要这个。 (untested) This will make info point to myStruct[structureArrayIndex] (Not copy the contents of myStruct[structureArrayIndex] to info). (未测试)这将使信息指向myStruct [structureArrayIndex](不将myStruct [structureArrayIndex]的内容复制到info)。 Pointers only point to things (like structs or some other type), they cannot contain a struct. 指针仅指向事物(如结构或其他类型),它们不能包含结构。

info = &myStruct[structureArrayIndex];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM