Я сделал эту C++ программу структур данных в основном правильной, однако у меня возникают проблемы с параметрами RefFunction()
. Они не разработаны должным образом. Они не должны проходить по стоимости, а скорее проходят по ссылке, и я не уверен, как это сделать. Он принимает ссылку на int
и ссылку на double
. Он запрашивает у пользователя ввод значений, которые будут храниться в переменных, на которые ссылаются его аргументы. Затем сможете вернуться в экземпляр класса и напечатать значения в main()
. Я очень благодарен за любую помощь, поскольку я очень застрял. Большое спасибо.
Заголовок файла:
#ifndef Prog1Class_h
#define Prog1Class_h
//A data structure of type Prog1Struct containing three variables
struct Prog1Struct
{
int m_iVal;
double m_dVal;
char m_sLine[81];
};
// A class, Prog1Class, containing a constructor and destructor
// and function prototypes
class Prog1Class
{
public:
Prog1Class();
~Prog1Class();
void PtrFunction(int *, double *);
void RefFunction(int, double);
void StructFunction(Prog1Struct *);
};
#endif
.CPP файл
#include "Prog1Class.h"
#include <string>
#include <iostream>
using namespace std;
Prog1Class::Prog1Class() {}
Prog1Class::~Prog1Class() {}
// PtrFunction shall query the user to input values to be stored in the
// variables referenced by it pointer arguments
void Prog1Class::PtrFunction(int *a, double *b)
{
cout << "Input keyboard values of type integer and double"<<endl;
cin>>*a >>*b;
}
// RefFunction shall be a C++ Reference function and shall query the user to
// input values to be stored in the variables referenced by it arguments
void Prog1Class::RefFunction(int a, double b)
{
cout << "Input keyboard values of type integer and double"<<endl;
cin >>a >>b;
}
// StructFunction shall query the user to input values to be stored in the
// three fields of the data structure referenced by its argument
void Prog1Class::StructFunction(Prog1Struct* s)
{
cout << "Input keyboard values of type integer and double"<<endl;
cin >>s->m_iVal>>s->m_dVal;
cout <<"Input a character string";
cin.ignore(1000, '\n');
cin.getline(s->m_sLine, 81, '\n');
}
В C++ вам не нужно использовать указатели для передачи по ссылке.
Объявите функцию следующим образом:
void Prog1Class::RefFunction(int& a, double& b)
Изменения, внесенные вами в и b внутри RefFunction
будут отражены в исходных переменных