简体   繁体   English

你如何在c ++中正确创建/传递引用参数?

[英]How do you properly create/ pass a reference argument in c++?

for my c++ assignment, I just have to create a 'char', print it, then pass it as a reference argument to a function, modify it, and reprint it to prove that it has been changed. 对于我的c ++赋值,我只需要创建一个'char',打印它,然后将它作为参考传递给函数,修改它,并重新打印以证明它已被更改。 It seems easy, and I'm probably making a really dumb mistake, but i keep getting an error that says 'unresolved externals'. 这似乎很容易,而且我可能犯了一个非常愚蠢的错误,但我不断收到一个错误,上面写着“未解决的外部因素”。 I've made a .cpp file and also declared a class in my header file. 我已经制作了一个.cpp文件,并在我的头文件中声明了一个类。

My .cpp file: 我的.cpp文件:

#include <iostream>
#include <fstream>
#include "referenceshw.h"

using namespace std;

int main(){

    char s = 's';
    char& s1 = s;
    Ref test;

    std::cout << s <<endl;
    test.modify(s);

}

void modify(char& s1){

    s1 = 'd';
    std::cout << s1 <<endl;
    std::cout << s <<endl;

}

My header file: 我的头文件:

#ifndef _REFERENCESHW_H
#define _REFERENCESHW_H

class Ref{

    public:
        char s;

void modify (char);

};

#endif

You function signatures dont match, in your .h you have: 你的签名功能不匹配,你的.h你有:

void modify (char);

and in .cpp 在.cpp

void modify(char& s1){

simply add & after char, in the .h 只需在.h中添加&之后的char

Also because the function is defined outside the class declaration you need to add Ref:: in front of modify in your .cpp. 另外,因为函数是在类声明之外定义的,所以需要在.cpp中的modify之前添加Ref ::。 In the end in your .cpp it should look like: 最后你的.cpp应该是这样的:

void Ref::modify(char& s1){

and in your .h 在你的.h

void modify(char&);

Borgleader is right. Borgleader是对的。 Other bugs: change 其他错误:改变

void modify(char& s1)

to

void Ref::modify(char& s1);

Also, are you trying to refer to a class member s1 in your code in modify()? 另外,您是否尝试在modify()中的代码中引用类成员s1?

s1 = 'd'; // this will change the parameter that was passed in, 
          // is that what you want?

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

相关问题 在C ++中使用typename作为函数参数时,如何传递引用? - How do you pass a reference when using a typename as a function argument in C++? C++:如何将文件作为参数传递? - C++: How do you pass a file as an argument? 如何将C ++引用传递给C函数的指针参数? - How do I pass a C++ reference, to a pointer argument of a C function? 在这种情况下,如何在C ++中正确传递成员函数作为参数? - How to properly pass member function as argument in this situation in C++? 如何通过引用从MQL4传递参数到C ++ DLL - How to pass argument by reference from MQL4 to C++ DLL 你如何强制编译器在C ++中通过引用传递一些变量? - How do you force compiler to pass some variable by reference in C++? 为什么或在什么情况下,您会将参数传递给函数作为C ++中的引用(或指针)? - Why, or in what situations, would you pass an argument to a function as a reference (or pointer) in C++? C ++,为什么可以将右值传递给以左值引用为参数的函数 - C++, why can you pass rvalue to a function which takes lvalue reference as argument 您如何正确地在 C++ 中执行 string.at(i)==“a” - How do you properly do string.at(i)==“a” in C++ C++ 引用和传递引用参数之间的区别 - C++ difference between a reference and a pass-by-reference argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM