1>------ Build started: Project: class_test, Configuration: Release Win32 ------
1>Build started 08-02-2015 18:58:34.
1>InitializeBuildStatus:
1> Creating "Release\class_test.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> stdafx.cpp
1> class_test.cpp
1>stdafx.obj : error LNK2005:
"private: void __thiscall ac::pf(unsigned char *)" (?pf@ac@@AAEXPAE@Z) already defined in class_test.obj
1>C:\Users\Kenneth\documents\visual studio 2010\Projects\class_test\Release\class_test.exe :
fatal error LNK1169: one or more multiply defined symbols found
Code compiles just fine. No issues here. When building, linker complains about this call to "pf(&a)"
#pragma once
//
typedef unsigned char byte;
// ac = abstract class
class ac{
public:
// pvf = pure_virtual_func
virtual void pvf() = 0;
virtual void vf1() = 0;
virtual void vf2(){
//
a = 10;
pf(&a); // <-this function call causes linker to complain as shown above
printf("%d", a);
};
private:
byte a;
void pf(byte *);
};
// I want to use scope resolution, so I can define "pf" outside the "ac" class body
// this goes for "vf2" as well all though this function is defined within the "ac" class body
// this was done to show what links without complains and what makes the linker complain
//
void ac::pf(byte *p){
//
a++;
}
It seems that functions declared within the "ac" class body can't easily be defined outside the class body. I want
to design the "ac" class this way or like something similar, and not within the class body itself.
How do I do this if possible. Looking forward to some in-depth knowledge here.
I assume the body for ac::pf is in a header? If so, then you have to mark it as inline:
1 2 3 4
inlinevoid ac::pf(byte *p){ // <- make it inline
//
a++;
}
Also... this is unrelated to your question... but...
1 2 3 4 5
// ac = abstract class
class ac{
public:
// pvf = pure_virtual_func
virtualvoid pvf() = 0;
This is a very bad habit. Rather than giving something an extremely vague name like 'ac' and adding comments to explain what 'ac' means... you should just give it a meaningful name:
1 2 3 4
class Abstract // <- no comment necessary
{
public:
virtualvoid pureVirtual() = 0; // <- no comment necessary
This makes your code infinitely easier to read and understand.