首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >链接错误.函数二叉树,派生类

链接错误.函数二叉树,派生类
EN

Stack Overflow用户
提问于 2014-01-26 07:21:33
回答 2查看 206关注 0票数 0

我想实现一个简单的二叉树,其中顶点包含lambdas。

代码语言:javascript
运行
AI代码解释
复制
class Element {
public:
    Element() { }
    virtual int operator()();
};

class Node : public Element {
private:
    std::function<int(Element, Element)> fn;
    Element left;
    Element right;
public:
    Node(std::function<int(Element, Element)> _fn, Element _left, Element _right)
        : fn(_fn), left(_left), right(_right) { }
    int operator()() { return fn(left, right); }
};

class Leaf : public Element {
private:
    int value;
public:
    Leaf(int _value) : value(_value) { }
    int operator()() { return value; }
};

int main() {
cout << Node([](Element a, Element b) { return a() + b(); }, Leaf(2), Leaf(5))() << endl;
return 0;
}

以下是我所犯的错误:

代码语言:javascript
运行
AI代码解释
复制
g++ -Wall -std=c++11 -Wextra -pedantic tree.cc -c
g++ -Wall -std=c++11 -Wextra -pedantic tree.o -o tree
tree.o: In function `main::{lambda(Element, Element)#1}::operator()(Element, Element) const':
tree.cc:(.text+0x18): undefined reference to `vtable for Element'
tree.cc:(.text+0x2a): undefined reference to `vtable for Element'
tree.o: In function `Element::Element()':
tree.cc:(.text._ZN7ElementC2Ev[_ZN7ElementC5Ev]+0xf): undefined reference to `vtable for Element'
tree.o: In function `Element::Element(Element const&)':
tree.cc:(.text._ZN7ElementC2ERKS_[_ZN7ElementC5ERKS_]+0x13): undefined reference to `vtable for Element'
tree.o: In function `Node::Node(std::function<int (Element, Element)>, Element, Element)':
tree.cc:(.text._ZN4NodeC2ESt8functionIFi7ElementS1_EES1_S1_[_ZN4NodeC5ESt8functionIFi7ElementS1_EES1_S1_]+0x4e): undefined reference to `vtable for Element'
tree.o:tree.cc:(.text._ZN4NodeC2ESt8functionIFi7ElementS1_EES1_S1_[_ZN4NodeC5ESt8functionIFi7ElementS1_EES1_S1_]+0x5a): more undefined references to `vtable for Element' follow
tree.o:(.rodata._ZTI4Leaf[_ZTI4Leaf]+0x10): undefined reference to `typeinfo for Element'
tree.o:(.rodata._ZTI4Node[_ZTI4Node]+0x10): undefined reference to `typeinfo for Element'
collect2: error: ld returned 1 exit status
make: *** [tree] Error 1

这意味着,类声明是否有问题,或者我不理解C++中函数式编程的某些方面?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-26 07:39:09

因为基类Element缺少函数调用virtual int operator()();操作符重载函数的实现,所以会收到链接错误。

与您的设计有关的其他问题

  1. 所有对基类的引用都应该是const引用's/Element/const Element&/‘
  2. 元素应该是ABC。函数调用重载应该是纯虚拟的。
  3. 函数调用重载应该是const类型。

改进型程序

代码语言:javascript
运行
AI代码解释
复制
#include <iostream>
#include <functional>
using namespace std;
class Element {
public:
    Element() { }
    virtual int operator()() const = 0;
};

class Node : public Element {
private:
    std::function<int(const Element&,const Element&)> fn;
    const Element& left;
    const Element& right;
public:
    Node(std::function<int(const Element&, const Element&)> _fn, const Element& _left, const Element& _right)
        : fn(_fn), left(_left), right(_right) { }
    int operator()() const { return fn(left, right); }
};

class Leaf : public Element {
private:
    int value;
public:
    Leaf(int _value) : value(_value) { }
    int operator()() const { return value; }
};

int main() {
    cout << Node([](const Element& a,const Element& b) { return a() + b(); }, Leaf(2), Leaf(5))() << endl;
    return 0;
}
票数 1
EN

Stack Overflow用户

发布于 2014-01-26 07:36:31

您尝试使用多态,但没有指针/引用。这不起作用:Why doesn't polymorphism work without pointers/references?

有效

代码语言:javascript
运行
AI代码解释
复制
Element left;
Element right;

都是Element类型(不是派生类型),您尝试对它们调用operator()。但是这个方法不是为Element实现的,这是错误消息告诉您的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21365173

复制
相关文章
Python在tkinter界面中显示matplotlib动画
在tkinter应用程序界面中同时显示matplotlib绘制的动态折线图、动态散点图和动态柱状图。
Python小屋屋主
2020/02/23
5.2K0
Matplotlib中中文不显示问题
我们在使用jupter进行数据分析的时候,会接触到Matplotlib这个库,它是用来进行可视化数据分析的,在一个图中,我们常常会加入一些中文来进行说明。当我们加入中文的时候会出现下图所示的样子:
荣仔_最靓的仔
2021/09/07
9920
Python 强制杀死运行中的多进程脚本
拉灯的小手
2023/06/20
2970
怎么在python中安装matplotlib_matplotlib依赖库
2.虽然下载Python的时候自带有pip,但这里更新一下pip,输入更新pip命令:
全栈程序员站长
2022/09/27
1.8K0
怎么在python中安装matplotlib_matplotlib依赖库
在Python Matplotlib中制作瀑布图
我们将用Python制作瀑布图,特别是使用matplotlib库。瀑布图显示了运行总数以及增减,这对于属性分析来说是很好的选择。
fanjy
2022/06/04
2.8K0
在Python Matplotlib中制作瀑布图
在DataGrid中显示图片
    DadaGrid 是 ASP.NET 编程中一个很重要的控件,其优良的可定制功能为提高它的表现力提供了极大的方便。除了与数据源直接绑定以外,我们还可以通过列绑定模板对 DataGrid 的列进行自定义,来按照我们设定的格式显示数据。
Java架构师必看
2021/03/22
3.7K0
解决matplotlib中文显示问题
Setting goals is the first step in turning the invisible into the visible.
小闫同学啊
2019/07/18
2.5K0
解决matplotlib中文显示问题
在matplotlib中关闭绘图轴的方法
# Keep making random walks, as long as the program is active
用户7718188
2021/10/08
2.3K0
【shell脚本】$ 在shell脚本中的使用
注释:$* 和 $@ 都表示传递给函数或脚本的所有参数,不被双引号(" ")包含时,都以"$1" "$2" … "$n" 的形式输出所有参数。但是当它们被双引号(" ")包含时,"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数;"$@" 会将各个参数分开,以"$1" "$2" … "$n" 的形式输出所有参数
码缘
2019/07/01
6.5K0
shell脚本中echo显示内容带颜色
shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033[0m" 其中41的位置代表底色, 36的位置是代表字的颜色 字背景颜色和文字颜色之间是英文的"" 文字颜色后面有个m 字符串前后可以没有空格,如果有的话,输出也是同样有空格 字颜色 30—37 echo -e "\033[30m 黑色字 \033[0m" ec
山海散人
2021/03/03
1.5K0
将Matplotlib绘制的图显示到Tkinter中(详细教程)
参考文献:https://blog.csdn.net/SHU15121856/article/details/87307124
全栈程序员站长
2021/04/07
4.2K0
velocity:在eclipse和ultraedit中增加对vm脚本语法的高亮显示支持
版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net/10km/article/details/52329820
10km
2019/05/25
1.5K0
trap - 在脚本中处理信号
  比如,按Ctrl+C会使脚本终止执行,实际上系统发送了SIGINT信号给脚本进程,SIGINT信号的默认处理方式就是退出程序。如果要在Ctrl+C不退出程序,那么就得使用trap命令来指定一下SIGINT的处理方式了。
yaohong
2019/09/11
1.7K0
matplotlib 设置绘图时显示中文
matplotlib绘图时,经常会遇到中文字体显示为方块的问题。网上有很多解决方案,比较普遍的是在代码里增加下面两行。
超哥的杂货铺
2020/07/28
1.1K0
关于opencv图片颜色不能正常在matplotlib中显示的问题
opencv默认的彩色图片的加载方式是按照BGR加载的,直接用opencv的函数展示是没有问题的,但是有时候我们想把多张图片放在一起展示,这时候用matplotlib就比较方便,但是matplotlib的图片展示是按照RGB展示的,如果中间不处理一下,直接展示opencv加载的图片,你会发现图片的颜色会出现问题,如何解决?
我是攻城师
2018/07/23
1.6K0
关于opencv图片颜色不能正常在matplotlib中显示的问题
Python | Mac下matplotlib中文显示乱码
解决matplotlib中文显示乱码找到一个简单的方法 对Mac来说,一部分字体放在了这里:/Library/Fonts/ 还有一部分在这里:/System/Library/Fonts/ 在使用的时候直接指定字体就好了: # coding=utf-8 from matplotlib.font_manager import FontProperties font = FontProperties(fname='/Library/Fonts/Songti.ttc') from matplotlib impor
MachineLP
2020/02/25
3.7K0
Python | Mac下matplotlib中文显示乱码
matplotlib圆环图/饼图显示比值
构建一个显示的数值的函数,将plt.pie中的autopct=该函数即可。 代码: import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.size'] = 7.0 # matplotlib设置全局字体 # 创建两组数据 x1 = [30,25, 66, 13, 23] x2 = [29, 28, 90, 19, 31] x_0 = [1,0,0,0] #用于显示空心
生信编程日常
2021/04/16
3.1K0
matplotlib圆环图/饼图显示比值
matplotlib无法显示图片_pycharm不出图
首先你运行之后最小化pycharm,看看是不是已经出来了,只是没有自己弹到最顶层。
全栈程序员站长
2022/09/25
2.2K0
matplotlib无法显示图片_pycharm不出图
点击加载更多

相似问题

AppleScript/Automator文件夹操作将Excel转换为CSV

14

在Automator中运行applescript

12

Automator,AppleScript还是其他?

10

Applescript在Automator中不会延迟

12

在AppleScript中从automator调用变量

14
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档