c ++编译时未定义的符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15486797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ undefined symbol when compiling
提问by LucienK
FIXED: had the method twice in the header file
修正:在头文件中有两次该方法
I get the following error when trying to compile my project
尝试编译我的项目时出现以下错误
% make
g++ -o p4 testTree.o tree.o node.o check.o
Undefined first referenced
symbol in file
Tree::inTree(int) tree.o
ld: fatal: Symbol referencing errors. No output written to p4
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `p4'
Makefile
生成文件
p4: testTree.o tree.o node.o check.o
g++ -o p4 testTree.o tree.o node.o check.o
testTree.o: testTree.cc tree.h node.h check.h
g++ -c -Wall -Werror testTree.cc
tree.o: tree.h tree.cc node.h check.h
g++ -c -Wall -Werror tree.cc
node.o: node.h node.cc check.h
g++ -c -Wall -Werror node.cc
check.o: check.h check.cc
g++ -c -Wall -Werror check.cc
clean:
rm -f *~ *.o p4
Relevant code from tree.cc and tree.h:
来自 tree.cc 和 tree.h 的相关代码:
tree.cc
树.cc
...
bool Tree::inTree(int k) const
{
return locate(k,root) != NULL;
}
...
tree.h
树.h
#ifndef TREE_H
#define TREE_H
#include "node.h"
#include "check.h"
using namespace std;
class Tree
{
private:
Node *root;
public:
Tree();
Tree(const Tree & t);
const Tree & operator=(const Tree &t);
friend ostream & operator<<(ostream &out, const Tree &t);
bool inTree(int k) const;
double & operator[](int k);
double & operator[](int k) const;
~Tree();
bool inTree(int index);
private:
Node * locate(int k, Node *rt) const;
ostream & display(ostream &out, Node *r, int dir=Node::L) const;
void add(int k, Node*&r);
void kill(Node *&rt);
void copy(Node *rt, Node *&newRt);
};
#endif
I get the feeling that it's something really simple, but I can't seem to figure it out.
我觉得这很简单,但我似乎无法弄清楚。
回答by Andy Prowl
The message you are getting actually comes from the linker, not from the compiler.
您收到的消息实际上来自链接器,而不是来自编译器。
One of your member functions, bool Tree::inTree(int index);
, is correctly declared and defined as a const
member function:
您的成员函数 之一bool Tree::inTree(int index);
已正确声明并定义为const
成员函数:
// Declaration in tree.h
bool inTree(int index) const;
// Definition in tree.cc
bool Tree::inTree(int k) const
// ^^^^^
However, in tree.h
you also define this non-const
overload of inTree()
:
但是,tree.h
您还定义了以下非const
重载inTree()
:
// Declaration in tree.h, definition (supposedly) nowhere
bool Tree::inTree(int k)
For which no definition is provided. This is what the linker complains about.
对于那些没有提供的定义。这就是链接器所抱怨的。
回答by bash.d
Here is your error:
这是你的错误:
bool Tree::inTree(int k) const
{
return locate(k,root) != NULL;
}
in your .h
you define
在你.h
你定义
bool inTree(int);
This is a difference!
这是一个区别!