How come when I have the same sort algorithm, as such
bool Sorter(const Object* n1, const Object* n2) { return (n1->GetValue() < n2->GetValue()); }
in separate .cpp files the linker (l believe) gives me a multiply defined symbol -error?
When I rename them (Sorter1, Sorter2, ..) it compiles. These classes are inherited from the same parent class but the parent has no algorithm stuff in there. I just fail to realize why this becomes an error when the classes have no direct contact to each other and am curious.
Also, is there any direct way of referencing sorters from say, a parent class, or do I just have to create a function which uses the sorter?
By default a function has external linkage, meaning any source file that has a declaration of the function can use it no matter which file has the definition. The linker is responsible for connecting the different uses, which is why it complains when it finds more than one.
To keep a function private to the source file it’s contained in you must use the
statickeyword on it or put it in an unnamed namespace.If all the function definitions are identical you can use the
inlinekeyword to indicate to the linker that they are identical, but in that case you should put the function into a header file to be completely safe.