簡體   English   中英

程序停止工作,我確定指針錯誤

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

我有這個程序,而且我仍然習慣於C ++指針,所以這可能是一個問題。 但是,當調用getStructData()函數時,程序崩潰。 我可能已經弄亂了與我所使用的結構的指針有關的內容,但是我目前還不確定。 任何提示或幫助表示贊賞。 謝謝,在人們開始瘋狂地投反對票之前,這不是我學校的作業,我只是在聖誕節假期里翻閱其他學校的作業來練習。

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

#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

您正在嘗試為空指針分配一個值。

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

因此在函數中,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)

認為您想要這個。 (未測試)這將使信息指向myStruct [structureArrayIndex](不將myStruct [structureArrayIndex]的內容復制到info)。 指針僅指向事物(如結構或其他類型),它們不能包含結構。

info = &myStruct[structureArrayIndex];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM