Click here to Skip to main content
15,913,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying this project for various programs now for list, search, etc. So, I intend to give it one last try to resolve the errors I am getting. I have implemented a generic list and I am trying to retrieve the data from a certain position in the list.
But I am getting an error: no matching function for call to 'List::retrieve(int&, Record&)'
Below is the code of main.cpp and a snippet of function retrieve from List.h
main.cpp
#include <iostream>
#include "List.h"    
#include "Key.h"
using namespace std;
typedef Key Record;
int main()
{
    int n;
    int p=3;
    List<int> the_list;
    Record data;
    cout<<"Enter the number of records to be stored. "<<endl;
    cin>>n;
    for(int i=0;i<n;i=i++)
    {
    the_list.insert(i,i);
    }
    cout<<the_list.size();
    the_list.retrieve(p, data);
    cout<<"Record value: "<<data;
    return 0;
}


List.h
Error_code retrieve(int position, List_entry &x)const
    {
    if(empty()) return underflow;
    if(position<0 || position>count) return range_error;
    x=entry[position];
    return success;
    }


Obviously, the List_entry is of type int and Record is not as from the error. Since Record is an alias of Key which wraps int, so I changed the second parameter in retrieve as this the_list.retrieve(p, data.the_key()); but it still gave the error.
Next, I changed List< int > to List < Key >but still the error exists.

For full code:
Main.cpp: http://pastebin.com/UrBPzPvi[^]
List.h: http://pastebin.com/7tcbSuQu[^]
Key.h http://pastebin.com/DrAH0MPq[^]
Key.cpp http://pastebin.com/xjTvgxxA[^]
Posted
Updated 4-Jan-11 5:36am
v5

Again? Oh, I see: this time the problem is a bit different:

In the function main, you declare an instance of List:

C++
List<int> the_list;


Must be:

C++
List<Record> the_list;


You're trying to use List<List_entry> where List_entry is Record, so you need to declare the type accordingly.
 
Share this answer
 
v2
Comments
optimus_prime1 3-Jan-11 0:05am    
I changed that to List< Record > the_list; and the_list.retrieve(p, data);
but it gives me a strange error : no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits](((std::basic_ostream<char,> >&)(& std::cout)), ((const char*)"Record value: ")) << data'|
Sergey Alexandrovich Kryukov 3-Jan-11 0:52am    
When reporting a compile-time error, you need also to point to a line of source code that caused this error.

Please see another answer. This is a separate answer, because you did not report the problem in first place and because this problem is unrelated to original question.
Manfred Rudolf Bihy 4-Jan-11 11:34am    
Good answer! 5+
This is another bug:

C++
cout<<"Record value: "<<data;


It does not compile because you're trying to call infix stream output operator "<<" with variable data of the type Record, but you never defined this operator for this type (how stream operator would know what do you mean by text output for this type?).

You don't have to define the operator. You can simply output separate member(s) of Record instead of whole Record:

C++
cout<<"Record value: "<<data.the_key();


Something like that.

Better now?
 
Share this answer
 
v3
Comments
optimus_prime1 3-Jan-11 1:24am    
It turns out that there is another error here in retrieve method if(empty()) return underflow; after changing to data.the_key(). :(
The error: passing 'const List' as 'this' argument of 'bool List<List_entry>::empty() [with List_entry = Key]' discards qualifiers|
It is rather not my mistake and I am tired but I was just following text book to learn these basics. Nae of text book: Data Structures and Program Design in C++ by Robert L Kruse, Alexander Ryba.
Sergey Alexandrovich Kryukov 3-Jan-11 3:25am    
Here is the deal. If just one error is fixed, please accept the answer. It's too boring. You should not say "It is rather not my mistake". If you post a problem, everything in your code is your responsibility. If you say that a book contains a mistake, your own mistake was copying the code without thinking. I understand it may be difficult for you to see the mistake, but it does not mean you did not make a mistake. "Just following text book" is not an excuse at all; it's rather a sign of lack of ability to read with understanding. You can learn better reading and understanding.

Then again, you received several examples of how to fix bugs based on compiler errors. Did you try to fix them yourself. Right now, let me tell you frankly what I think: your performance is below normal. If you expect such a detailed help on every step, you will never learn anything real.

I'm giving you too poor help just because I'm started to follow baby steps with you. I don't like this; I like to give good help. I'm not even your tutor. So, how shall we proceed?
Manfred Rudolf Bihy 4-Jan-11 11:35am    
Good call! 5+
A bonus advice to my answer (sorry I repeat myself, but this time I'll make in more clear).

You really, really need to get rid of Error_code. Here is how:

C#
List_entry retrieve(int position) const
{
    if (empty()) throw "underflow";
    if (position<0 || position>count) throw "range error";
    return entry[position];
}


This throw part is simplified, just for example. Instead of string, it should better be some class or different classes for different errors; for range error it should be initialized with valid range and actual value of position.

The benefits of exceptions are so overwhelming, that the techniques which involve returning of error information makes no sense anymore, since structural exception handling was invented (for CLU language, circa 1970) by Barbara Liskov, a Turing Award laureate (2008).

Unfortunately, this is no time to explain all the techniques and the motivation, but there is a huge choice of literature on the topic. (I am not familiar with the introductory literature; I am one of the early implementors of the technology when it was not yet introduced in C++ -- believe or not.)
 
Share this answer
 
Comments
Kasson 3-Jan-11 0:46am    
Good Call
Sergey Alexandrovich Kryukov 3-Jan-11 1:00am    
Oh, thank you very much for good words, Kasson.
Sergey Alexandrovich Kryukov 5-Jan-11 13:37pm    
optimus_prime1
Ok, on you latest comment and compiler message take the following assignment.

Google the following:

"C++" error "discards qualifiers"


This query will return you good number of quite relevant documents. Learn how to fix it. Don't just follow the pattern, try to understand. Report back on your progress.

Honestly, this is the best I can do for you. If I fix every bug one by one for you, it will never end. If I take the whole code and put it right at once, you will not learn anything. (Needless to say it would be dishonest and not fair to your teachers and fellow students.) Please understand it. Please also understand you tend to take up too much time for the matter not quite appropriate for this site because of its level and not interesting or useful to most of the readers.

If you cannot accept this game, I cannot see any interest in helping you. Probably your teachers receive some salary for their work. Please understand this, too.

Thank you.
 
Share this answer
 
Comments
optimus_prime1 4-Jan-11 9:59am    
:P So, at last I did that!
bool empty()const // that's it :P
{
}
I do have any tutor/teacher/instructor since I am a non IT student but I do understand that doing thing oneself if the only way to learn program
Thanks!
Sergey Alexandrovich Kryukov 4-Jan-11 16:09pm    
Correct answer! Now, did you really understand why?

I have checked your code in full, added all my changes and made sure it compiles now (not so sure about run-time).

For a record: my compiler does not issue "discard qualifiers", but instead shows much more comprehensible error message, so trying this by yourself could give you more reassurance.
By the way, what's you compiler? Also, I suspect it only shows first compile-time error and stops, probably that's why kept asking one question after another instead all at once. There must be a compiler option to show as many errors as possible at once -- more convenient in many cases.

I'm glad you did it.

Was that useful? Please accept the answer now.
Sergey Alexandrovich Kryukov 5-Jan-11 13:37pm    
@optimus_prime1: are you accepting this answer as well?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900