传递结构

时间:2016-05-27 08:12:44

标签: c++ reference structure

当我尝试运行此代码时,我不知道为什么会出现此错误...当我传递结构参考时,这有什么问题......

这是错误:

undefined reference to citire(type)

代码:

#include <iostream>

using namespace std;

struct type {
    int x[500] = {0};
    int y[500] = {0};
    int lx = 0;
    int ly = 0;
    int aparitii[10000] = {0};
};

void citire(type s);
bool estePrim(type s);
int sumaCfr(type s);
void createY(type s);
void printY(type s);

int main()
{
    type s;

    citire(s);
    cout<<"X LENGTH = "<<s.lx<<endl;
    return 0;
}

void citire(type &s)
{
    int i = -1;
    cin>>s.x[++i];
    while (s.x[i] != 0) {
        cout<<"Insert " << i + 1<< " value"<<endl;
        cin>>s.x[++i];
    }
    s.lx = i;
}

2 个答案:

答案 0 :(得分:2)

因为函数声明和定义的参数类型不匹配。 type(即按值传递)和type&(即通过引用传递)不是一回事。

如果您想通过引用传递它,则需要使它们保持一致,然后将声明更改为:

void citire(type& s);

答案 1 :(得分:2)

您的功能声明应与您的实施相对应

...
void citire(type& s);
...
int main()
{
...
}

void citire(type& s)
{
    int i = -1;
    cin>>s.x[++i];
    while (s.x[i] != 0) {
        cout<<"Insert " << i + 1<< " value"<<endl;
        cin>>s.x[++i];
    }
    s.lx = i;
}