c++ - Overriding Functions result in multiple definitions error
问题描述
I am attempting to setup an Employee base case and a Hourly/Salaried/Commissioned Employee subclasses. When I try to compile I am getting the following errors:
HourlyEmployee.o: In function `getInfo()':
/cygdrive/d/HourlyEmployee.cpp:4: multiple definition of `func1()'
Employee.o:/cygdrive/d/subclass.cpp:4: first defined here
HourlyEmployee.o: In function `getEarning()':
/cygdrive/d/HourlyEmployee.cpp:9: multiple definition of `func2()'
Employee.o:/cygdrive/subclass.cpp:9: first defined here
Below is my code, it is very bare as I am simply trying to setup inheritence/overriding of functions.
subclass.cpp
#include "subclass.h"
string func1(/* arguments */) {
/* code */
return 0;
}
double func2(/* arguments */) {
/* code */
return 0;
}
What am I missing here?
解决方案
When defining a member function, yuo have to use the ClassName::
notation, i.e.
#include "HourlyEmployee.h"
string HourlyEmployee::getInfo(/* arguments */) {
/* code */
return 0;
}
double HourlyEmployee::getEarning(/* arguments */) {
/* code */
return 0;
}
推荐阅读
- c++ - 为什么 std::rel_ops 需要相等运算符?
- php - 添加 Serializable 接口后 unserialize() 停止工作
- java - 在 Selenium 中从文件中设置 Cookie
- docker - Docker 将间歇性故障推送到 kubernetes 上的私有 docker 注册表(docker-desktop)
- caching - GatsbyJS Service Worker 配置不尊重“networkFirst”,继续提供陈旧数据
- flutter - 如何在 Flutter 转换之间显示进度轮/进度条?
- php - CodeIgniter 表单未提交
- jquery - 如何在元素多个动作中定义变量
- python - 如何检查 Checkbutton 的值?
- c++ - (Raspberry Pi 3)如何使用 set() 函数(来自 raspicam/raspicam_cv.h)设置相机属性?