Go Down

Topic: library: const declaration how to do right? (Read 843 times) previous topic - next topic

s-light

Jun 17, 2012, 12:24 pm Last Edit: Jun 17, 2012, 01:17 pm by s-light Reason: 1
Hi Community,

iam writing a Library out of my developing sketch - (transfer functionality to library thing)

my problem is - i want to use constants to define the used pins of an object of the library.

so i declared constant variables in my 'MotorFaderControl.h' file like this:
Code: [Select]

/**
 * Class Definition:
 **/
class MotorFaderControl
{
public: // public methods
//Constructor
MotorFaderControl(byte iPin_ServoTrack, byte iPin_Direction, byte iPin_Speed, byte iPin_Break);
//Destructor
~MotorFaderControl();
// get Current Position
word getCurrentPosition();
private: // per object data
const byte pin_ServoTrack;
const byte pin_Direction;
const byte pin_Speed;
const byte pin_Break;
word wCurrentPosition;
unsigned long ulTimeLastAction;
// private methods
word getPosDif(word wValue1, word wValue2);
};

in the .cpp file i want to set them this way:
Code: [Select]

MotorFaderControl::MotorFaderControl(byte bPin_ServoTrack, byte bPin_Direction, byte bPin_Speed, byte bPin_Break)
{
pin_ServoTrack = bPin_ServoTrack;
pin_Direction = bPin_Direction;
pin_Speed = bPin_Speed;
pin_Break = bPin_Break;

wCurrentPosition = 0;
ulTimeLastAction = 0;

/* I have read that this initialization should be not in the Contstructor but better in an 'begin' function.
pinMode(pin_ServoTrack, INPUT);
pinMode(pin_Direction, OUTPUT );
pinMode(pin_Speed, OUTPUT );
pinMode(pin_Break, OUTPUT );
ulTimeLastAction = millis();*/
}

but when i try to compile my library-test sketch
Code: [Select]


/****************************************************************************************************
  MotorFaderControl Library Test sketch
  This sketch should Control a Motor of a Motorfader.
 
  Created:  09.06.2012
  copyright by Stefan Krueger
  written by Stefan Krueger s-light.eu
 
  Last modified: 17.06.2012 12:00
  By Stefan Krueger s-light.eu
 
  changelog / history :
  09.06.2012 00:00  created library out of developing project 'Motorfader_Test_2012_06_09__11_45__v17.ino'
  09.06.2012 15:30  created test sketch
  10.06.2012 12:00  first tests
   
****************************************************************************************************/

#include <MotorFaderControl.h>

/**
 * Fader
 * Constructor : (byte bPin_ServoTrack, byte bPin_Direction, byte bPin_Speed, byte bPin_Break);
 **/
MotorFaderControl myMFC_Fader1(A2, 12,  3, 9);
MotorFaderControl myMFC_Fader2(A3, 13, 11, 8);

void setup()  
{
/**************************************************/
/* Setup Pins                                     */
/**************************************************/


/**************************************************/
/* Initialise all the Variables                   */
/**************************************************/

//myMFC_Fader1.begin();
//myMFC_Fader1.begin();

/**************************************************/
/* Welcom @ Start Up!                             */
/**************************************************/

Serial.begin(9600);
Serial.println();
Serial.println("Welcome to MotorFaderControl Library Test Sketch!");
Serial.println("system is starting.!");
Serial.println(":-)");
Serial.println();
Serial.println("Go!");
}

void loop()
{
delay(500);
}

i get the following errors:
Code: [Select]
MotorFaderControl.cpp: In constructor 'MotorFaderControl::MotorFaderControl(byte, byte, byte, byte)':
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_ServoTrack' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Direction' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Speed' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Break' with 'const' type 'const byte'
..
MotorFaderControl.cpp:66: error: assignment of read-only data-member 'MotorFaderControl::pin_ServoTrack'
MotorFaderControl.cpp:68: error: assignment of read-only data-member 'MotorFaderControl::pin_Direction'
MotorFaderControl.cpp:69: error: assignment of read-only data-member 'MotorFaderControl::pin_Speed'
MotorFaderControl.cpp:70: error: assignment of read-only data-member 'MotorFaderControl::pin_Break'


if i understand this thread right - it was the same / similar problem -
but i dont understand how to get this to work for me -

can someone help to understand the thing?
what is the right way to get the idea of use constant declarations to work with the library and the constructor initialization?!

sunny greetings
stefan

[edit]thanks to PaulS i had the wrong error messages copied.. now thats right..[/edit]

PaulS

Code: [Select]
error: uninitialized member 'MotorFaderControl::cwFaderDriveTimeOut'
That member is not even mentioned in the code you posted.

Code: [Select]
error: assignment of read-only data-member 'MotorFaderControl::cwFaderDriveTimeOut'
Neither is this member.

s-light

Sorrry - thats the fool of copy and paste and try to make things easy to understand -
thanks PaulS for the correction - i will edit the original post - it  is the same error white some more definitions..^^
'right' errors:
Code: [Select]

MotorFaderControl.cpp: In constructor 'MotorFaderControl::MotorFaderControl(byte, byte, byte, byte)':
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_ServoTrack' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Direction' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Speed' with 'const' type 'const byte'
MotorFaderControl.cpp:56: error: uninitialized member 'MotorFaderControl::pin_Break' with 'const' type 'const byte'
..
MotorFaderControl.cpp:66: error: assignment of read-only data-member 'MotorFaderControl::pin_ServoTrack'
MotorFaderControl.cpp:68: error: assignment of read-only data-member 'MotorFaderControl::pin_Direction'
MotorFaderControl.cpp:69: error: assignment of read-only data-member 'MotorFaderControl::pin_Speed'
MotorFaderControl.cpp:70: error: assignment of read-only data-member 'MotorFaderControl::pin_Break'

PaulS

Quote
error: assignment of read-only data-member 'MotorFaderControl::pin_ServoTrack'
error: assignment of read-only data-member 'MotorFaderControl::pin_Direction'
error: assignment of read-only data-member 'MotorFaderControl::pin_Speed'
error: assignment of read-only data-member 'MotorFaderControl::pin_Break'

These don't really seem too hard to understand. The const keyword you are using makes them read-only.

Why do you think you need to make the member fields const? Why are the constructor arguments not const?

s-light

the idea was to define all things const that can not change -so that the compiler could do some cleanup or something like this..
so if i mark the constructor arguments as const nothing changes in the errors.
Code: [Select]
MotorFaderControl::MotorFaderControl( const byte cbPin_ServoTrack, const byte cbPin_Direction, const byte cbPin_Speed, const byte cbPin_Break)

is it the wrong way to declare the members constant?

and yes the 'error: assignment of read-only data-member 'MotorFaderControl::xxx' is clear - but i dont know how to set the 'const' in an other way...
i hope my approach is clear?!

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview