I have my class in 2 different files(header and source-file) and i have 2 variables which is a const but the only way to give the const variable a value is to do it trough an constructor initializer.

But how do i implement an constructor initializer in my class ?

Do i need to declare the constructor initializer or do i only need to implement it or both?

Here is an example :

//Sqrt.h
class Sqrt{
private:
 const float originalValue;
 const float sqrtValue;
public:
 Sqrt();
 Sqrt(float initValue);
 float sqrtValue();
 float originalValue();
}
//Sqrt.cpp
Sqrt::Sqrt() 
: originalValue(0.0f), sqrtValue(0.0f) //initializer list
{
}
Sqrt::Sqrt(float value)
: originalValue(value), sqrtValue( std::sqrt(value) ) //initializer list
{ /* empty */ }

Hmmm i made my class thesame as your example but i get 3 errors saying "First defined here"

I dont know if my code will help you or not but ill post it anyway

//header.h
class MotionState : public btMotionState
{

public:

   MotionState(const btTransform& initalTransformation, irr::scene::ISceneNode* const node) ;
   void getWorldTransform(btTransform& worldTrans) const ;

   void setWorldTransform(const btTransform& worldTrans);

private:

   irr::scene::ISceneNode* const node;

   irr::core::matrix4 matr;

   btTransform initalTransformation;
};
//source.cpp
MotionState::MotionState(const btTransform& initalTransformation, irr::scene::ISceneNode* const node)
 : node(node), initalTransformation(initalTransformation)
   {

   }

   void MotionState::getWorldTransform(btTransform& worldTrans) const
   {
      worldTrans = this->initalTransformation;
   }

   void MotionState::setWorldTransform(const btTransform& worldTrans)
   {
      worldTrans.getOpenGLMatrix(matr.pointer());

      this->node->setRotation(matr.getRotationDegrees());
      this->node->setPosition(matr.getTranslation());
   }

Why would you want to initialize your const variable in the constructor class? Since they are constant and you know the values beforehand, isn't better to assign the value to your const variable at the point they are declared?

The problem is that here :

MotionState::MotionState(const btTransform& initalTransformation, irr::scene::ISceneNode* const node)
 : node(node), initalTransformation(initalTransformation)

Take "node(node)" as an example. Which one is your private member variable
and which one is the parameter variable ? You might know but the compiler does
not. So you need to change some names, and make then different. Try something like
this :

MotionState::MotionState(const btTransform& InitalTransformation, irr::scene::ISceneNode* const Node)
 : node(Node), initalTransformation(InitalTransformation)
{}

i did as you said but i am still getting thesame errors :

In function `_ZN3irr4core12irrAllocatorIcE12internal_newEj':
first defined here
In function `_ZN3irr4core12irrAllocatorIcE12internal_newEj':|
first defined here|
In function `_ZN3irr4core12irrAllocatorIcE12internal_newEj':|
first defined here|
||=== Build finished: 3 errors, 0 warnings ===|

Edited 6 Years Ago by Martje: n/a

Well the bug I found was a subtle one because its not being complained by the compiler.
The error message you are getting is because some sort of linking issues. Which
function is "_ZN3irr4core12irrAllocatorIcE12internal_newEj" and where is it defined?
I am guessing when you include"xxx.h", where xxx contains the _ZN3irr4core12irrAllocatorIcE12internal_newEj function, the compiler is defining it
more than once.

This article has been dead for over six months. Start a new discussion instead.