C++ 将结构传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15181765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:08:01  来源:igfitidea点击:

Passing structs to functions

c++struct

提问by Phorce

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)

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

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

我已经尝试过这里解释的一些示例:Simple way to pass临时结构 by value in C++?

Hope someone can Help me.

希望可以有人帮帮我。

回答by Nikos C.

First, the signature of your data() function:

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

bool data(struct *sampleData)

cannot possibly work, because the argument lacks a name. When you declare a function argument that you intend 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 structat all actually. So this can simply become:

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

bool data(sampleData *samples)


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

其次,此时sampleDatadata() 不知道该结构体。所以你应该在此之前声明它:

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:

最后,您需要创建一个类型为 的变量sampleData。例如,在您的 main() 函数中:

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.

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

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:

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

// 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);
}

回答by Inisheer

bool data(sampleData *data)
{
}

You need to tell the method which type of struct you are using. In this case, sampleData.

您需要告诉方法您正在使用哪种类型的结构。在这种情况下,示例数据。

Note: In this case, you will need to define the struct prior to the method for it to be recognized.

注意:在这种情况下,您需要在要识别的方法之前定义结构。

Example:

例子:

struct sampleData
{
   int N;
   int M;
   // ...
};

bool data(struct *sampleData)
{

}

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

      sampleData sd;
      data(&sd);

}

Note 2: I'm a C guy. There may be a more c++ish way to do this.

注2:我是一个C家伙。可能有一种更 c++ 的方式来做到这一点。

回答by Hossein Jandaghi

Passing structs to functions by reference: simply :)

通过引用将结构传递给函数:简单地:)

#define maxn 1000

struct solotion
{
    int sol[maxn];
    int arry_h[maxn];
    int cat[maxn];
    int scor[maxn];

};

void inser(solotion &come){
    come.sol[0]=2;
}

void initial(solotion &come){
    for(int i=0;i<maxn;i++)
        come.sol[i]=0;
}

int main()
{
    solotion sol1;
    inser(sol1);
    solotion sol2;
    initial(sol2);
}