Hi
I have a question. The aswer is very short I think. I got many different useful helps in different forums but still my main question is not answered clearly!
MY QUESTION:
Can a not inline method have internal linkage?
This question is simple & clear (if you don't think that I may mean something else).
But let me give you an example in which I need the answer of this question. This is just an example of where I need the answer. It is not my main issue. And I have other examples where the aswer is needed.
by this example I want only to show how important my question is:
Example:
Suppose I have a project with only two .cpp files:
console.cpp:
#include <iostream>
using namespace std;
class A
{
void f();
};
void A::f()
{
cout<< "hello";
}
int main()
{
}
unit1.cpp:
#include <iostream>
using namespace std;
class A
{
void f();
};
void A::f()
{
cout<< "bye";
}
In this project we have two .cpp files. Each .cpp file has a method with name A::f(). The method in Each file is independent, that is, each translation unit has its own definition for A::f().
This project can be compiled without errors. But it can't be linked. It has this linker error(s):
Error 1 error LNK2005: "private: void __thiscall A::f(void)" (?f@A@@AAEXXZ) already defined in console.obj unit1.obj
Error 2 fatal error LNK1169: one or more multiply defined symbols found G:\Important Files\My Documents\Visual Studio 2008\Projects\console app\Release\console app.exe 1
This error can be solved if A::f() has internal linkage.
If A::f() was not a method and was a global function then I could use the keyword static to let it have internal linkage & then there was no linker errors. But it is not a global function. It is a method.
How can I give internal linkage to a method?
Note: There are many different ways to avoid this linker error. For example I can rename one A::f(). Or I can inline A::f(). Or I can use namespaces. Or I can use a Borland linker. But my question is not how solve this error.
My question is: How can I make the likage internal?
Can a method have have internal linkage?
I hope someone can answer my question a clear as I said!
(Why the daniweb editor is not rich text?! I remember it was before)