Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

public function is undeclared identifier

Hi,
 'maximum' is a public class function and when I compile my program is has one error: 'maximum' : undeclared identifier
I think the problem is how i put maximum in main, can you please adise how can i declare my function in main so it works.

int List:: maximum (ListItemType newItem)

{

ListNode *cur;

//cur=head;

int max=0;

for (cur=head; cur!=NULL;cur=cur->next)
{
      if (newItem>cur->item )
            max=newItem;
}


return max;

}

int main ()

{
//      List string:

    ListItemType number;
   // int count;
    //Node * head=NULL;

   while ( number != 0 ) {

    cout<<"Please enter numbers to be inserted,(0 to end): "<<endl;
    cin>>number;

    cout<<maximum(number);

   }
      return 0;
}
0
Gipsy
Asked:
Gipsy
  • 3
  • 3
  • 2
1 Solution
 
minjiberCommented:
the prob is that maximum is a function of class List. to use this function u should declare a class list

i.e:

List a; //for example

then to use the function use:

cout<<a.maximum(number);

thats the right way to use it, but u created a class List and didnt use it in ur main, so i'm not sure what exactly ur trying to do. if u need more help just send us what exactly u need ur prog to do.

0
 
bcladdCommented:
(1)  maximum is part of List so you need to call it with a list object:
  string.maximum(number);

(2) string is NOT a good name for a variable (there is a standard type std::string). You might want to use a more descriptive name.

-bcl
0
 
GipsyAuthor Commented:
Hi it seems to work with no errors, I think my maximum function is correct, it is finding the largest nuber in the list, but the result I get from main is 0 or a weird numeric number. Can you please advise what  the problem could be? is my function correct at all ?
Thank you.
int List:: maximum (ListItemType newItem)

{

ListNode *cur;

int max;

for (cur=head; cur!=NULL; cur=cur->next)
{
     if (newItem>cur->item )
        max = newItem;
      
}

return max;

}

int main ()

{
//     List string:
List a;

ListItemType number;
int index=1;
   // int count;
    //Node * head=NULL;

   while ( number != 0 ) {

    cout<<"Please enter numbers to be inserted,(0 to end): "<<endl;
    cin>>number;

   }
   a.insert(index,number);
   cout<<"Maximum number is" <<a.maximum(number);
   return 0;
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bcladdCommented:
(1) You never initialize max (the return value) inside of maximum. I would think that what you want in maximum is:

int List:: maximum () // the method doesn't actually use its parameter so get rid of it
{
  if (head == NULL)
    return 0; // or some other value; make sure the list has members

  ListNode *cur;
  int max = head->item; // initialize max to one of the possible values it might have
  for (cur=head; cur!=NULL; cur=cur->next)
  {
       if (max < cur->item )
          max = newItem;    
  }
  return max;
}

Note that I have changed the semantics a little bit; your version takes a value (the one just inserted) as a parameter and doesn't really use it for much. My version traverses the entire list setting max to the largest item value in the list. Thus my version should only be called AFTER the whole list is built.

As to building the list, you want ot make sure that you have a.insert INSIDE the while loop so that you actually build a list. Then the call to maximum is outside of the loop (after the whole list was built).

-bcl
0
 
GipsyAuthor Commented:
I am writing a minimum function as well. The problem I have now is that if I enter  0, my minimum number will always be 0 due to condition in main.
What would be the best way to loop the message asking for the list numbers and then to terminate easily ?

0
 
bcladdCommented:
(1) If you want to use a sentinel value (an end-of-input marker value), then you will want to choose one that is not a valid input value. If negative values are not permitted, check for an input value less than 0 and terminate on that.

(2) An end-of-file controlled loop can work when all the possible input values are valid (when there is no good sentinel value). The user can enter an end-of-file character to mark the end of the input (platform dependent on how to do that).

(3) Could also read until there is a problem reading from the stream. User could cause that problem by typing a non-integer value like "done". The read fails and you can then reset the stream and read the word if you want more input from the user. Loop would look like:

  cout << "Number: ";
  while (cin >> number) {
    // process number (add it to the list or whatever)
    cout << "Number: "; // print next prompt
  }
  // We know cin is either EOF or there was a problem
  // We can check or clear the flags here or ignore them
  // if we don't care what is going on there.

Hope this helps,
-bcl
0
 
minjiberCommented:
this is a good solution for the minimum:

int List:: minimum (ListItemType newItem)

{

ListNode *cur=head;

int min=cur->item;

for (cur=head->next; cur!=NULL; cur=cur->next)
{
     if (newItem<cur->item )
        min = newItem;
     
}

return min;

}

but i dont think ur main is good, just from what i've seen below, the insert func in main is not included in the loop i.e its:

while ( number != 0 ) {

    cout<<"Please enter numbers to be inserted,(0 to end): "<<endl;
    cin>>number;

}
a.insert(index,number);
   
instead of:

while ( number != 0 ) {

    cout<<"Please enter numbers to be inserted,(0 to end): "<<endl;
    cin>>number;
    a.insert(index,number);

}
     
so only the last number is included in the list. and by the way what's the index for??

i hope this helps
0
 
minjiberCommented:
sorry man, forget the min i gave u use this, coz it seems there's  a prob in ur max too. both min and max should not take a value, they should only return a value. below is the min and max:

int List:: minimum()
{

ListNode *cur=head;

int min=cur->item;

for (cur=head->next; cur!=NULL; cur=cur->next)
{
     if (cur->item<min )
        min = cur->item;
     
}

return min;

}

int List:: maximum()
{

ListNode *cur=head;

int max=cur->item;

for (cur=head->next; cur!=NULL; cur=cur->next)
{
     if (cur->item>max )
        max = cur->item;
     
}

return max;

}

and there's a prob in ur main about inserting the items, i hope u checked it.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now