簡體   English   中英

帶結構和指針的程序出錯(Structures,C ++)

[英]Error in program with structures and pointers (Structures, 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是指向供應商的指針。 *p是供應商。 p->amt是一個int。

所以當你想要p指向的對象的amt ,你可以用兩種方式之一來做: p->amt(*p).amt

您可以使用p->amt(*p).amt修復代碼。 *p->amt*(p->amt)無效。

p是供應商的對象,其類型是指針。 “ - >”用於使用指針對象。 所以使用p-> amt。

你也可以使用(* p).amt。

更新答案:

min的變化缺失了。 請用這個:

int min = p->amt ;

或使用此:

int min = (*p).amt;

low()函數中缺少min的聲明。

int min = (p->amt);

這應該可以幫助您編譯代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM