Solved

public function is undeclared identifier

Posted on 2004-08-05
11
302 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now