gpt4 book ai didi

C++ 数据结构编程 : Passing Values by Reference?

转载 作者:行者123 更新时间:2023-11-28 03:07:16 24 4
gpt4 key购买 nike

<分区>

我已经完成了这个 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's 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's 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');
}

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