Solved

public function is undeclared identifier

Posted on 2004-08-05
11
321 Views
Last Modified: 2010-04-01
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
Comment
Question by:Gipsy
  • 3
  • 3
  • 2
11 Comments
 
LVL 2

Expert Comment

by:minjiber
ID: 11729490
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
 
LVL 11

Expert Comment

by:bcladd
ID: 11729506
(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
 

Author Comment

by:Gipsy
ID: 11734026
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 11

Expert Comment

by:bcladd
ID: 11735407
(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
 

Author Comment

by:Gipsy
ID: 11746088
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
 
LVL 11

Accepted Solution

by:
bcladd earned 40 total points
ID: 11746584
(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
 
LVL 2

Expert Comment

by:minjiber
ID: 11756111
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
 
LVL 2

Expert Comment

by:minjiber
ID: 11756187
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question