表达式必须具有类类型(使用结构数组作为函数参数)
Expression Must Have Class Type (Using a struct array as a function parameter)
我正在Visual Studio Pro 2013中编写一个C++程序,在尝试在函数调用中使用结构数组作为参数时遇到错误。这是我的代码。
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:nn"
<< "A) Calculate the distance from a specified set of coordinates.n"
<< "B) Print out the information for all waypoints.nn"
<< "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;
我在 dataInfo.latitude 和 dataInfo.longitude 上的 distanceCalculator 函数调用中收到错误,上面写着"表达式必须具有类类型"。
为什么我会收到此错误以及如何解决它?
变量 dataInfo
是一个 ProcessingData
数组。类ProcessingData
具有成员latitude
。
你似乎想要处理一个由dataInfo
元素的latitude
成员组成的数组,但这样的数组不存在。可以获取结构的成员,但不能使用相同的语法在一个步骤中从数组的所有元素中提取该成员。
如果要传递数组,则有几个选择。传递数组很容易:
void foo(int A[])
{
...
}
int A[8];
foo(A);
问题是该函数很难知道数组的大小,因此很容易越界。我们可以为数组的大小添加另一个参数:
void foo(int A[], unsigned int nA)
{
...
}
int A[8];
foo(A, 8);
或开始使用标准容器:
void foo(vector<int> A)
{
...
}
vector<int> A;
foo(A);
ProcessingData
的每个实例都包含一个用于纬度的double
和一个用于经度的double
。
这里没有double
数组。似乎您正在尝试通过从中选择所有 Latitude 双精度数组来自动"查看"dataInfo
,但它不是那样工作的。
最简单的解决方案可能是将函数更改为:
double distanceCalculator(ProcessingData data[], double inLat, double inLong)
{
// logic here using things like data[5].latitude
}
相关文章:
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- 如何使用函数的输出初始化 const 数组结构字段?
- 传递数组结构、ofstream 和 interger 以运行
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 将文本文件读取到数组结构中
- C/C++中数组结构和数组结构的通用接口
- C++ MDC final-在字符类型的数组结构中按字母顺序对记录中的名称进行排序
- C++ 使用数组结构创建平衡的二叉搜索树
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- 使用 vector 在 c++ 中声明 3D 数组结构
- 数组结构无法正确打印
- 如何在C++中访问数组结构内部的数组结构
- CIN进入数组结构似乎什么也没输入
- ifstream将数组结构到txt文件中,然后尽可能将其提取为数组
- wlanapi-将WlanFreeMemory释放其WLAN_INTERFACE_INFO数组结构
- 显示数组结构 c++
- C++ 使用函数访问数组结构的方法是什么?
- 将庞大的数组结构复制到 GPU
- 如何封送包含字符矩阵的数组结构
- C++多维数组结构的对齐