Jump to content
  • Advertisement
Sign in to follow this  

Inline member function

This topic is 3991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't know what I'm doing wrong here; I had the same issue recently, but can't remember what I did to fix it. I didn't get anywhere with reducing it to a minimal example, so I'll just show exactly what I have. material.h
#ifndef MATERIAL_H
#define MATERIAL_H

#include "Colour.h"

namespace Graphics
	class LightTransportParameters
		float diffuse_;
		float specular_;

		void normalizeAndSetReflection(float diffuse, float specular);

		float transparency;

		LightTransportParameters(float diffuse, float specular, float transparency)
			: transparency(transparency)
		{ normalizeAndSetReflection(diffuse, specular); }

		float diffuse() { return diffuse_; }
		float specular() { return specular_; }

	struct Material
		LightTransportParameters lightTransportParameters;
		float refractiveIndex;

		Colour colour;

		Material(LightTransportParameters lightTransportParameters, float refractiveIndex, Colour colour)
			: lightTransportParameters(lightTransportParameters), refractiveIndex(refractiveIndex), colour(colour)

#include "Floating.h"
#include "Material.h"

inline void Graphics::LightTransportParameters::normalizeAndSetReflection(float diffuse, float specular)
	if (Utility::floatEqual(diffuse + specular, 1.0f))

	float k = 1 / (diffuse + specular);
	diffuse *= k;
	specular *= k;

	diffuse_ = diffuse;
	specular_ = specular;
#include <iostream>
#include "Sphere.h"

int main()
	using namespace Graphics;
	using Maths::Vector3;

	Sphere s(Vector3(0, 0, 0), 1.0f, Material(LightTransportParameters(0.5f, 0.5f, 0.0f), 1.0f, Colour(1.0f, 0, 0)));
main.obj : error LNK2019: unresolved external symbol "private: void __thiscall Graphics::LightTransportParameters::normalizeAndSetReflection(float,float)" (?normalizeAndSetReflection@LightTransportParameters@Graphics@@AAEXMM@Z) referenced in function "public: __thiscall Graphics::LightTransportParameters::LightTransportParameters(float,float,float)" (??0LightTransportParameters@Graphics@@QAE@MMM@Z)
Obviously the problem is with the declaration of the inline member function, but I'm clearly missing something obvious!

Share this post

Link to post
Share on other sites
I think the problem is that the function is defined as "inline", but not declared as "inline" in the header.

Regardless of that "normalizeAndSetReflection" can never be inlined (except maybe inside of material.obj), because its definition is not in the header file.

In order to have it inlined everywhere, you have to move the definition into the header file.

Share this post

Link to post
Share on other sites
Original post by AshleysBrain
An easy and obvious solution - just remove the inline keyword! The compiler will probably inline it anyway if it's beneficial...

inline methods must be visible to each translation unit that uses it. By placing your inline method in the .cpp the material translation unit is the only unit that has it visible. In your case you want to move your inline definition to the header file.

Also, I wouldn't really trust the compiler to inline methods. For across translation unit inline analysis you need to enable whole program optimizations which many compilers don't support and the ones that do don't do it very well. Aditionally, even for methods that are declared inline (or a force inline equivelent) don't always inline even when it is a good idea to inline. For example, GCC is very terrible at inling methods while Intel is very good.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!