首页 > 解决方案 > 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?

标签: c++inheritanceoverriding

解决方案


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;
}

推荐阅读