我有一個結構,其中包含三個變量下的對象列表。名稱,註冊號,金額。程序結構和指針錯誤(結構,C++)
struct vendor
{
int reg, amt;
char add[30];
}list[10];
我已經做了一個函數來找到最小量(amt),使用引用的概念。
int low(vendor *p, int n)
{
int i;
min = (p->amt);
for(i =1;i<n;i++)
{
if(min > *(p->amt))
{
min = *(p->amt);
}
p++;
}
return min;
}
在主我已經包括語法:
low(list, n);
我得到一個錯誤:
Invalid argument of unary '*' operator.
我已經使用點運算符也試過,不工作。 這是我的第一個函數結構指針程序。
請您指出代碼中的錯誤。
非常感謝你
阿努邦
(更新)的完整代碼:
#include <iostream>
using namespace std;
struct vendor
{
int reg, amt;
char add[30];
}list[10];
int low(vendor *p, int n)
{
int i;
min = (p->amt);
for(i =1;i<n;i++)
{
if(min > (p->amt))
{
min = (p->amt);
}
p++;
}
return min;
}
int main()
{
int i;
int fa;
int n,fr;
cin >> n;
for(i =0;i<n;i++)
{
cin >>list[i].reg>>list[i].add>>list[i].amt;
// Enter reg no. , address and amount.
}
low(list, n); // Calling function
for(i = 0;i<n;i++)
{
if(fr == list[i].amt)
// This is to check for position of least amount.
// For printing the reg no. and address of least amt.
{
fa = i;
}
}
cout << fr <<"\n" << fa <<endl;
// print the reg no. and address of least amt.
}
錯誤:
Overloaded function with no contextual type information.
Invalid operands of types <unresolved overloaded function
Cannot resolve overloaded function
'p-> amt'的類型爲'int'。表達式'*(p-> amt)'將其視爲指針。而不是'*(p-> amt)'只需在代碼中寫入'p-> amt'。 – Peter
我收到錯誤。請檢查更新的問題。謝謝。 –
你從來沒有爲你的'min'變量聲明類型,所以編譯器假定你正在引用'min'函數。通過正確聲明'min'來修正錯誤。 –