Sign in to follow this  
Silly_con

Inline members, why this doesn't work ?

Recommended Posts

I am wondering, why this preprocessor "jungle" doesn't work, it seems to be fine, but linker errors:
/********************************************************/
/* Foo.h
/********************************************************/
#ifndef _FOO_H_
#define _FOO_H_

#ifdef USING_INLINE
#define INL __forceinline
#else
#define INL 
#endif

class Foo {
private:
	int n;
public:
	Foo() {};
	Foo(int a) { n = a; };

	INL int func(int a) const;
	INL const Foo& operator = (const Foo& f);
	INL const Foo  operator + (const Foo& f) const;
};

#ifdef USING_INLINE
#include "Foo.inl"
#endif

#endif // FOO

/********************************************************/
/* Foo.cpp
/********************************************************/

#include "Foo.h"

#ifndef USING_INLINE
int Foo::func(int a) const {
    return n*a;
}

const Foo& Foo::operator = (const Foo& f) {
	n = f.n;
	return *this;
}

const Foo Foo::operator + (const Foo& f) const {
	return Foo(n + f.n);
}
#endif


/********************************************************/
/* Foo.inl - basically the same as the .cpp file
/********************************************************/
#ifdef USING_INLINE
int Foo::func(int a) const {
	return n*a;
}

const Foo& Foo::operator = (const Foo& f) {
	n = f.n;
	return *this;
}

const Foo Foo::operator + (const Foo& f) const {
	return Foo(n + f.n);
}
#endif

/********************************************************/
/* main.cpp
/********************************************************/

// -- comment or uncomment following line
#define USING_INLINE 
#include "Foo.h"

void main() {
   Foo f,g;
   Foo h(1);

   f.func(1);
   h = f + g;
   // ...
}

Its supossed that the compiler, compile .cpp file first, so if USING_INLINE is no defined, it compiles nothing, but i get duplicated names linker error. I am using msvc6 & msvc 2003, althought you think that try to do this is stupid, I want to do it, I need to do it, and I know all the theory of good design of the manual of good c++ programmer, not to inline in excess, inline only the needed, premature optimization etc... If you know where is the fail, or you have a better solution to do the same, please tell me! thanks !!

Share this post


Link to post
Share on other sites
You define USING_INLINE in the main.cpp module, so when the foo.h file gets included into the main.cpp module, the inlined versions of the functions get added. The problem is that in your foo.cpp module, you're not defining this constant, so the regular versions get added in the foo.cpp module, hence they're defined twice in two seperate modules, and when it tries to link them, it finds the duplicate. You'd need to have another header file that both modules include, in which you either do or don't define this constant.

Oh, and as a sidenote, you have a double guard against the foo.inl file. You check if USING_INLINE is defined both in the foo.h file before you include the file, and you have the same guard again inside the foo.inl file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this