gpt4 book ai didi

c++编译时 undefined symbol

转载 作者:太空狗 更新时间:2023-10-29 19:40:38 25 4
gpt4 key购买 nike

已修复:头文件中有两次方法

在尝试编译我的项目时出现以下错误

% 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'

生成文件

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

相关代码来自tree.cc和tree.h:

树.cc

...
bool Tree::inTree(int k) const
{
return locate(k,root) != NULL;
}
...

树.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

我觉得这很简单,但我似乎无法弄明白。

最佳答案

您收到的消息实际上来自链接器,而不是来自编译器。

您的成员函数之一 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
// ^^^^^

但是,在 tree.h 中,您还定义了 inTree() 的非 const 重载:

// Declaration in tree.h, definition (supposedly) nowhere
bool Tree::inTree(int k)

未提供定义。这就是链接器所提示的。

关于c++编译时 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15486797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com