将结构传递给函数 [英] Passing structs to functions

查看:151
本文介绍了将结构传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何将结构(通过引用)传递到函数,以便可以填充结构的成员函数。到目前为止,我写了:

  bool data(struct * sampleData)
{

}

int main(int argc,char * argv []){

struct sampleData {

int N;
int M;
string sample_name;
string speaker;
};
data(sampleData);

}

我遇到的错误是:


C ++需要所有声明的类型说明符
bool data(const& testStruct)


我已经尝试过这里解释的一些例子:简单的方法是通过C ++中的值传递临时结构体



希望有人可以帮助我。


<首先,你的data()函数的签名:

  bool data(struct * sampleData)

不可能工作,因为参数缺少名称。当你声明一个你想实际访问的函数参数时,它需要一个名字。所以改成类似:

  bool data(struct sampleData * samples)

但在C ++中,实际上不需要使用 struct 。所以这可以简单地成为:

  bool数据(sampleData * samples)
pre>




其次, sampleData struct不为数据所知() 在那时候。所以你应该在之前声明它:

  struct sampleData {
int N;
int M;
string sample_name;
string speaker;
};

bool data(sampleData * samples)
{
samples-> N = 10;
samples-> M = 20;
//等
}

最后, sampleData 的变量。例如,在main()函数中:

  int main(int argc,char * argv []){
样本数据样本;
data(& samples);
}

注意,您需要将变量的地址传递给data函数,因为它接受一个指针。



但是,请注意,在C ++中,你可以通过引用直接传递参数,不需要用指针模拟它。你可以这样做:

  //注意参数是通过引用获取的(前面的& $ b $ of the argument name。)
bool data(sampleData& samples)
{
samples.N = 10;
samples.M = 20;
//等
}

int main(int argc,char * argv []){
sampleData samples;

//不需要在这里传递一个指针,因为data()通过引用获取
//传递的参数。
data(samples);
}


I am having trouble understanding how to pass in a struct (by reference) to a function so that the struct's member functions can be populated. So far I have written:

bool data(struct *sampleData)
{

}

int main(int argc, char *argv[]) {

      struct sampleData {

        int N;
        int M;
        string sample_name;
        string speaker;
     };
         data(sampleData);

}

The error I get is:

C++ requires a type specifier for all declarations bool data(const &testStruct)

I have tried some examples explained here: Simple way to pass temporary struct by value in C++?

Hope someone can Help me.

解决方案

First, the signature of your data() function:

bool data(struct *sampleData)

cannot possibly work, because the argument lacks a name. When you declare a function argument that you intent to actually access, it needs a name. So change it to something like:

bool data(struct sampleData *samples)

But in C++, you don't need to use struct at all actually. So this can simply become:

bool data(sampleData *samples)


Second, the sampleData struct is not known to data() at that point. So you should declare it before that:

struct sampleData {
    int N;
    int M;
    string sample_name;
    string speaker;
};

bool data(sampleData *samples)
{
    samples->N = 10;
    samples->M = 20;
    // etc.
}

And finally, you need to create a variable of type sampleData. For example, in your main() function:

int main(int argc, char *argv[]) {
    sampleData samples;
    data(&samples);
}

Note that you need to pass the address of the variable to the data() function, since it accepts a pointer.

However, note that in C++ you can directly pass arguments by reference and don't need to "emulate" it with pointers. You can do this instead:

// Note that the argument is taken by reference (the "&" in front
// of the argument name.)
bool data(sampleData &samples)
{
    samples.N = 10;
    samples.M = 20;
    // etc.
}

int main(int argc, char *argv[]) {
    sampleData samples;

    // No need to pass a pointer here, since data() takes the
    // passed argument by reference.
    data(samples);
}

这篇关于将结构传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆