Pages: [1]   Go Down
Author Topic: library: const declaration how to do right?  (Read 681 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/**
 * 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:
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:

/****************************************************************************************************
   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:
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]
« Last Edit: June 17, 2012, 06:17:37 am by s-light » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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'
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?!
Logged

Pages: [1]   Go Up
Jump to: