Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

public function is undeclared identifier

Posted on 2004-08-05
11
Medium Priority
?
342 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 160 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

963 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