I isolated a part of the code to quote for you, but found it you are
correct. The linking error I get is coming from a more complicated
situation that also involves a second class and a friend declaration.
Here is an abridged version (sorry I can't make it shorter):
==================================================================================
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
// #include "Window_Mgr.h" // for 2nd version of program
class Screen
{
// for 2nd version of program
// friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index,
// Window_Mgr::index, Screen&);
friend class Window_Mgr;
public:
typedef std::string::size_type index;
char get() const
{ return contents_[cursor_]; }
char get(index r, index c) const;
private:
std::string contents_;
index cursor_;
index height_, width_;
};
// inline declared in the class declaration; no need to repeat on the
definition
char Screen::get(index r, index c) const
{
index row = r * width_; // compute the row location
return contents_[row + c]; // offset by c to fetch specified
character
}
#endif
==================================================================================
#ifndef WINDOW_MGR_H
#define WINDOW_MGR_H
#include <string>
class Screen;
class Window_Mgr
{
public:
typedef std::string::size_type index;
Window_Mgr& relocate(Window_Mgr::index, Window_Mgr::index, Screen&);
private:
};
#endif
==================================================================================
// Window_Mgr.cpp
#include "Window_Mgr.h"
#include "Screen.h"
Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r,
Window_Mgr::index c, Screen& s)
{
// ok to refer to private members height and width of Screen class
s.height_ += r;
s.width_ += c;
return *this;
}
==================================================================================
// Screen Main.cpp
#include "Screen.h"
#include "Window_Mgr.h"
int main()
{
Window_Mgr w;
Screen s;
}
==================================================================================
This is the error I get:
Window_Mgr.obj : error LNK2005: "public: char __thiscall
Screen::get(unsigned int,unsigned int)const " (?get@Screen@@QBEDII@Z)
already defined in Screen Main.obj
1>C:\Documents and Settings\T\My Documents\Visual Studio 2005\Projects
\Language Tests\Debug\overload inline.exe : fatal error LNK1169: one
or more multiply defined symbols found
==================================================================================
I don't know why inlining the get(index, index) method fixes the
linking error.
I'm sure I am doing something wrong. Maybe it is the way I structure
the program to be able to use the friend declaration.
The error also goes away if I don't inline get(index, index) but take
out the lines involving Screen in the "Screen Main.cpp" file.
==================================================================================
2nd version of program:
It's even more complicated if instead of the "friend class Window_Mgr"
declaration, I make Window_Mgr::relocate a friend. In this case, I
get two linking errors:
1>Screen.obj : error LNK2005: "public: char __thiscall
Screen::get(unsigned int,unsigned int)const " (?get@Screen@@QBEDII@Z)
already defined in Screen Main.obj
1>Window_Mgr.obj : error LNK2005: "public: char __thiscall
Screen::get(unsigned int,unsigned int)const " (?get@Screen@@QBEDII@Z)
already defined in Screen Main.obj
==================================================================================
These errors are also fixed if I inline the get(index, index) method.
If I don't inline get(index, index) but take out the lines involving
Screen in the "Screen Main.cpp" file, I still get this error:
1>Window_Mgr.obj : error LNK2005: "public: char __thiscall
Screen::get(unsigned int,unsigned int)const " (?get@Screen@@QBEDII@Z)
already defined in Screen.obj