Reputation: 11
I am writing a C++ program in Visual Studio Pro 2013, and I am having an error when trying to use a structure array as a parameter in a function call. This is my code.
struct ProcessingData
{
double latitude;
double longitude;
};
double distanceCalculator(double dataLat[], double dataLong[], double inLat, double inLong)
{
//some code in here
}
int main()
{
char userChoice;
double userLatitude;
double userLongitude;
ProcessingData dataInfo[834];
cout << "Welcome!\n"
<< "Please select which option you would like to run:\n\n"
<< "A) Calculate the distance from a specified set of coordinates.\n"
<< "B) Print out the information for all waypoints.\n\n"
<< "Please enter your selection now: ";
cin >> userChoice;
switch (userChoice)
{
case 'a': //Menu option for distance calculation
{
getTheFile(); //other function that is being used
cout << "Please enter the latitude for the coordinate you are using: ";
cin >> userLatitude;
cout << "Please enter the longitude for the coordinate you are using: ";
cin >> userLongitude;
distanceCalculator(dataInfo.latitude[], dataInfo.longitude, userLatitude, userLongitude)
}
break;
I am getting an error in my distanceCalculator function call, on the dataInfo.latitude and dataInfo.longitude that says "expression must have class type."
Why am I getting this error and how to I solve it?
Upvotes: 0
Views: 981
Reputation: 141554
Each instance of a ProcessingData
contains one double
for Latitude and one double
for Longitude.
There are no arrays of double
here. It seems as if you are trying to automagically "view" an array of double by selecting all the Latitude doubles from dataInfo
, however it doesn't work that way.
Your simplest solution will probably be to change the function to be:
double distanceCalculator(ProcessingData data[], double inLat, double inLong)
{
// logic here using things like data[5].latitude
}
Upvotes: 1
Reputation: 99094
The variable dataInfo
is an array of ProcessingData
. The class ProcessingData
has a member latitude
.
You seem to want to deal with an array consisting of the latitude
members of the elements of dataInfo
, but no such array exists. You can take a member of a struct, but you can't use the same syntax to extract that member from all elements of the array in one step.
If you want to pass an array, you have a couple of options. Passing the array is easy:
void foo(int A[])
{
...
}
int A[8];
foo(A);
The trouble is that the function has difficulty knowing the size of the array, so it is easy to go out of bounds. We can add another parameter for the size of the array:
void foo(int A[], unsigned int nA)
{
...
}
int A[8];
foo(A, 8);
or start using the standard containers:
void foo(vector<int> A)
{
...
}
vector<int> A;
foo(A);
Upvotes: 1