未定义对班级成员的引用 [英] Undefined reference to class member
问题描述
我制作了一个类定义文件Key.h及其代码Key.cpp.
我给Record命名为typedef Key Record.当我在main.cpp中创建Record对象作为代码Record data;时.我收到一条错误消息:未定义对Key :: Key(int)的引用".
Key.cpp类中还有另一个错误,指出未在此范围内声明x".我在这里放置了这些文件的代码,并在注释中也写了关于错误的内容.
Key.h
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
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
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
非常感谢您的帮助!
Main.cpp: http://pastebin.com/R8RQyGVs
Key.h http://pastebin.com/DrAH0MPq
Key.cpp http://pastebin.com/xjTvgxxA
Thanks a lot for help!!
推荐答案
应该不是
should''nt that be
int Key::the_key()const
{
return key;
}
对于其余的,我不知道"List"是什么...,所以我无法编译其余的代码.
for the rest, I don''t know what "List" is ... so I cannot compile the rest of the code.
我可以在您描述的代码中准确地看到两个错误:
实际上,x
不在Key::the_key
的范围内:
I can see exactly two errors in the code you describe:
Indeed,x
is not in the scope ofKey::the_key
:
int Key::the_key()const
{
//no declaration of x anywhere is scope
//did you mean key (private field)?
return x;
}
错误消息"Undefined reference to Key::Key(int)
"与以下内容有关:
The error message "Undefined reference to Key::Key(int)
" is related to the following:
class Key
{
public:
Key(int x=0);
//...
};
构造函数键已声明,但我在任何地方都找不到构造函数的主体.
不幸的是,当您定义类时,使用的C ++编译器不会报告此类问题.它仅在使用该类时报告该问题;然后调用没有主体的构造函数或其他函数.换句话说,当静态地知道它们从未被调用时,编译器允许在没有主体的情况下声明(非虚拟)函数.
如您所愿,您需要在Key.cpp
中定义一个主体,或在Key.h
中内联它:
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
:
class Key
{
public:
Key(int x=0) { key = x; }
//...
};
顺便说一句,您已经使用名称x
作为要分配给key
的参数的事实,这向我暗示,这也是您遇到"x was not declared in this scope
"问题的原因.也许,您以某种方式无法理解功能参数范围仅限于此功能(或构造函数)?如果是这样,您如何想象它会起作用?
而已.谢谢.
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.
这篇关于未定义对班级成员的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!