Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;数据结构编程:通过引用传递值? 我做过的C++数据结构程序基本上是正确的,但是我在 ReFunStUnCENE()/参数上遇到了麻烦。它们设计得不太恰当。它们不应该是按值传递的,而是按引用传递的,我不知道如何做到这一点。它引用一个int,并引用一个double。它查询用户输入要存储在其参数引用的变量中的值。然后能够返回类实例并在main()中打印值。我非常感谢你的帮助,因为我被困住了。多谢各位_C++_Function_Class_Pass By Reference - Fatal编程技术网

C++;数据结构编程:通过引用传递值? 我做过的C++数据结构程序基本上是正确的,但是我在 ReFunStUnCENE()/参数上遇到了麻烦。它们设计得不太恰当。它们不应该是按值传递的,而是按引用传递的,我不知道如何做到这一点。它引用一个int,并引用一个double。它查询用户输入要存储在其参数引用的变量中的值。然后能够返回类实例并在main()中打印值。我非常感谢你的帮助,因为我被困住了。多谢各位

C++;数据结构编程:通过引用传递值? 我做过的C++数据结构程序基本上是正确的,但是我在 ReFunStUnCENE()/参数上遇到了麻烦。它们设计得不太恰当。它们不应该是按值传递的,而是按引用传递的,我不知道如何做到这一点。它引用一个int,并引用一个double。它查询用户输入要存储在其参数引用的变量中的值。然后能够返回类实例并在main()中打印值。我非常感谢你的帮助,因为我被困住了。多谢各位,c++,function,class,pass-by-reference,C++,Function,Class,Pass By Reference,头文件: #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

头文件:

#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'); 
}
#包括“Prog1Class.h”
#包括
#包括
使用名称空间std;
Prog1Class::Prog1Class(){}
Prog1Class::~Prog1Class(){}
//PTR功能应查询用户输入要存储在系统中的值
//指针参数引用的变量
void prog1类::ptr函数(int*a,double*b)
{
cout>*b;
}
//FFFUNTUCTONE应是C++参考函数,并应向用户查询
//要存储在其参数引用的变量中的输入值
void prog1类::RefFunction(int a,double b)
{
cout>b;
}
//StructFunction应查询用户输入要存储在
//由其参数引用的数据结构的三个字段
void Prog1Class::StructFunction(Prog1Struct*s)
{
cout m_iVal>>s->m_dVal;

C++中的CUT

,不需要使用指针来传递引用。 像这样声明函数:

void Prog1Class::RefFunction(int& a, double& b)

您在
RefFunction
中对a和b所做的更改将反映在原始变量中

请避免使用指针:在您的情况下,它只是void fn(int&,double&)。传递值不错,但更改不会传递给调用方:“void Prog1Class::RefFunction(int a,double b)非常感谢,程序现在编译和运行正确。“在C++中,你不需要使用指针来传递引用。”我会说你从来没有“使用指针通过引用”。