Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a class definition file Key.h and its code Key.cpp.
I have given alias to Record as code typedef Key Record. When I create an object of Record in main.cpp as code Record data;. I get an error saying “Undefined reference to Key::Key(int)”.
There's another error in class Key.cpp saying “x was not declared in this scope”. I have put up the code of these files here and have also written about error in the comments.
Key.h
class Key
{
int key;
public:
static int comparisons;
Key(int x=0);
int the_key()const;
};
bool operator == (const Key &x,const Key &y);
bool operator >= (const Key &x,const Key &y);
bool operator <= (const Key &x, const Key &y);
bool operator > (const Key &x, const Key &y);
bool operator < (const Key &x, const Key &y);
bool operator != (const Key &x, const Key &y);


Key.cpp
#include"Key.h"
int Key::comparisons=0;
int Key::the_key()const
{
    return x;  //Something wrong here I think?
}
bool operator == (const Key &x, const Key &y)
{
    Key::comparisons++;
    return (x.the_key()==y.the_key());
}


main.cpp
#include <iostream>
#include "List.h"
#include "Key.h"
using namespace std;
typedef Key Record;
int main()
{
    int n;
    List<int> the_list;
    Record data; // Underfined refernece to Key::Key(int)
    cout<<"Enter the number of records to be stored. "<<endl;
    cin>>n;
    for(int i=0;i<n;i>    {
    the_list.insert(i,i);//(position, List_entry &x)
    }
    cout<<the_list.size();
    cout<<"Record value: ";
    return 0;
}</int></iostream>



Main.cpp: http://pastebin.com/R8RQyGVs
Key.h http://pastebin.com/DrAH0MPq
Key.cpp http://pastebin.com/xjTvgxxA
Thanks a lot for help!!
Posted

should'nt that be

C#
int Key::the_key()const
{
    return key; 
}


for the rest, I don't know what "List" is ... so I cannot compile the rest of the code.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jan-11 14:10pm    
I already answered this before; and you did not answer the other question...
I can see exactly two errors in the code you describe:

Indeed, x is not in the scope of Key::the_key:
C++
int Key::the_key()const
{
    //no declaration of x anywhere is scope
    //did you mean key (private field)?
    return x; 
}


The error message "Undefined reference to Key::Key(int)" is related to the following:

C++
class Key
{
public:
   Key(int x=0);
//...
};


Constructor Key is declared, but I did not find the body of the constructor anywhere.

Unfortunately, C++ compiler you use does not report such problem when the class is defined; it only reports the problem when the class is used; and the constructor or other function without a body is called. In other words, the compiler allows declarations of (non-virtual) functions without a body when it is statically known they are never called.

As you probably intended, you need to either define a body in you Key.cpp or inline it in Key.h:

C++
class Key
{
public:
   Key(int x=0) { key = x; }
//...
};


By the way, the fact you've use the name x for a parameter to be assigned to key hints me that this is also a reason for your problem with “x was not declared in this scope”. Maybe, you somehow fail to understand that a function paramer scope is limited to this function (or constructor)? If so, how would you imagine it could work?

That's it. Thank you.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Jan-11 14:40pm    
optimus_prime1, please accept the answer if it resolves your problem.
Manfred Rudolf Bihy 2-Jan-11 17:18pm    
I've already had to do with OP on one of his previous questions (related to this one), he seems to be having a hard time learning this stuff, but he did mention he was using a book to learn. Maybe he needs somebody to teach him how to read. But I still think he deserves credit for trying!
Sergey Alexandrovich Kryukov 2-Jan-11 22:22pm    
Yes, Manfred, this member seemingly makes me troubles. Will you look at another question of optimus_prime1 I answered (http://www.codeproject.com/Answers/141831/Generic-contigous-list-implementation.aspx#answer3)? In addition to my answer I listed several problems (look just at those redundant "include" that make code non-compiled) and... received votes of "1" and "2"; and one commenter argues that my answer is "technically perfect" but "useless", because it would not help OP as OP is considered as a beginner. Very nice! (See my comment.)

At the same time, I agree with you that OP deserved some credit. In next question, optimus_prime1 presented just a bit more accurate code. Maybe, this activity is not 100% useless...
optimus_prime1 2-Jan-11 22:44pm    
I didn't downvote anybody! Why are getting annoyed with me over that :-(
Sergey Alexandrovich Kryukov 2-Jan-11 22:52pm    
optimus_prime1,

I beg you pardon, then. If you say so, of course I trust you -- my mistake.
Also, I would advice you to attend to the points I criticize, not to take criticism as offense and ask further questions if you really don't understand something. After all, I'm really trying to help effectively, think you can improve, grow and wish you only the best.

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