将数组的元素传递给C ++中的函数

时间:2019-07-19 18:46:05

标签: c++ c++11

我需要将一个结构数组的元素传递给函数。但是我仍然做不到。

Person是一个嵌套结构。

//define Address structure
struct Address
{
    string Street;
    string City;
    string PostalCode;
};

//define Person structure
struct Person
{
    string Name;
    int Age;
    float Salary;
    struct Address Address;
};

// How I invoque the function
cout << "Address: " << ConcateAddress(persons[i].Address) << endl;


// Return a concatenated string with completed address of persion
string ConcateAddress(struct Address address)
{    
    string adressFinal=  address.Street + " " + address.PostalCode;
    if(!address.City.empty()){
        adressFinal = adressFinal + ", "+ address.City;
    }
}

1 个答案:

答案 0 :(得分:3)

您必须在函数定义的末尾添加return语句。您已成功将array的元素传递给该函数,但您的函数未返回任何值。

string ConcateAddress(struct Address address)
{    
    string adressFinal=  address.Street + " " + address.PostalCode;
     if(!address.City.empty()){
          adressFinal = adressFinal + ", "+ address.City;
     }
    return adressFinal;    //return statement
}