Description of this problem is little longer, here it is:
I have a console application under Visual C++ 6.0. It consists of main file "main.cpp" which uses a class defined in two files as usual: "someclass.h" and "someclass.cpp". Finally there is "library.h" file, which contains support functions (as an object too) for someclass (the class defined in someclass.x files).
Files contain this code:
library.cpp
Code:
//this is the library needed by someclass; implementation goes right after
//declaration here, all in one file; I use this (declaration & implementation
//in one file) often and that's is not the problem...or it is?
#pragma once //doesn't help me
#ifndef _AVOID_MULTIPLE_LINKING_ //even this
#define _AVOID_MULTIPLE_LINKING_
class AnyOtherClass {
public: void method();
};
//linker reports:
//error LNK2005: "public: void __thiscall CSomeClass::method(void)" (?method@CSomeClass@@QAEXXZ) already defined in main.obj
void AnyOtherClass::method() {
{}
}
#endif
someclass.h
Code:
//this is a header file for someclass
#include "library.h" //this library is needed by someclass
//class is not really declared here
//not even implemented in the following cpp file
//that's just for simplicity
//code for someclass - only declarations
//...
//...
someclass.cpp
Code:
//this is a source file for someclass, so this line must be here:
#include "someclass.h"
//code for some class - implementation
//...
//...
main.cpp
Code:
//finally I'd like to use someclass (which uses AnyOtherClass) in a console application like this
#include "someclass.h"
int main() {
return 0;
}
//but I can't compile it...
/*
I found out this solution: Join someclass.h and someclass.cpp into one header file, keep this
file linked to main.cpp and keep the include directive ("library.h") on the top of it too - it works.
But I'd like to know what is wrong and how to make it work with someclass.h and someclass.cpp files
separately.
*/