Reputation: 25
My function reads data from file and it works well, but when I return data something goes wrong. This is how it looks like:
Code:
struct csvh
{
//struct declaration
};
float glowa_csv(csvh CSVHead, float EndOfHead)
{
ifstream obraz;
obraz.open("POP_OCT.csv", ios::binary);
//wczytywanie naglowka
obraz.read(CSVHead.data, sizeof(CSVHead.data)-1);
obraz >> CSVHead.lateral_range;
obraz.read(CSVHead.lat_px_size, sizeof(CSVHead.lat_px_size)-1);
obraz >> CSVHead.lateral_pxsize;
... ( and so on)
EndOfHead=obraz.tellg();
obraz.close();
cout << CSVHead.data<<endl<<CSVHead.godzina<<(rest of variables)<<endl;
return CSVHead.lateral_range, CSVHead.lateral_pxsize, (...), EndOfHead;
}
int main()
{
float EndOfHead;
csvh CSVHead;
glowa_csv(CSVHead, EndOfHead);
system("Pause");
system("cls");
cout << CSVHead.data<<endl<<CSVHead.godzina<<(...)<<endl;
return 0;
}
Upvotes: 1
Views: 95
Reputation: 137158
If you want to return multiple values from a function you must either return a class/struct or pass the variables by reference.
Passing by reference is perfectly valid but can be confusing for some people, so returning a complex object might be the "better" solution. You can return values in Tuple:
std::tuple<int,int,int> method(int input)
{
/* do your stuff here */
return { errorCode , value1 , value2 };
}
so you don't have to create extra types. Obviously the exact types in the tuple will depend on what you need to return
Upvotes: 4
Reputation: 180935
First off you cannot return multiple values from a function like
return CSVHead.lateral_range, CSVHead.lateral_pxsize, (...), EndOfHead;
This winds up only returning EndOfHead
thanks to the comma operator. What it looks like you want to do is pass CSVHead
and EndofHead
by reference so that the function modifies the variables you pass to it and the changes are reflected in main()
. You can change the function to
void glowa_csv(csvh &CSVHead, float &EndOfHead)
And get rid of the return statement entierly.
Upvotes: 3