Methods and parameters declaration problem in a class with header

夙愿已清 提交于 2021-02-11 14:46:55

问题


Helo everyone,

I'm having big trouble with the declaration of some parameters in a class I defined in a header file

I'm using a raspberry pi with C++ and trying to implement some objects through a class.

Therefore, I have :

  • my main cpp file where I call the object (it works, I'm sure of that, you'll see)

  • the class cpp file called "Motorcontrol01.cpp"

  • the header file called "Motorcontrol01.h"

My code is more complex than what I show here, but I simplified it for tests purpose and it don't work anyway -_- :

"Motorcontrol01.cpp" :

//###################################################################################
// Class for creating the different shift register entry data
//###################################################################################

#include <iostream> //for Cout to display stuff
#include <chrono>   //library for counting time
#include <thread>   //for multithreading
#include <vector>
#include <wiringPi.h>   //Raspberry pi GPIO Library
#include "MotorControl01.h" //looking in current dirrectory

MotorControl::MotorControl(){   //constructeur
    std::vector<std::vector<bool>> DataArr(16, std::vector<bool>(8, 0));    //the vector of vectors with boolean values in it for transmitting the motors control data
    //###################################################################################
    //  GPIO Pins definition
    //###################################################################################
    int SPser = 13, SPclk = 19, SPrclk = 26; //Define the output pins used for the main shift registers
    int PWMSPclk = 17, PWMSPrclk = 18;  // define the output pins to control the PWM shift registers
}

void MotorControl::SendDataPWM(std::vector<std::vector<bool>> DataArr){

    // Initialize wiringPi and allow the use of BCM pin numbering
    wiringPiSetupGpio();

    //initialize the pins
    pinMode(SPser, OUTPUT);
    pinMode(SPclk, OUTPUT);
    pinMode(SPrclk, OUTPUT);
    pinMode(PWMSPclk, OUTPUT);
    pinMode(PWMSPrclk, OUTPUT);

while (1) {
    digitalWrite(PWMSPclk, HIGH);       //sets the pin to a HIGH value
    digitalWrite(PWMSPclk, LOW);
    std::cout << "PWM SP CLK"<<  PWMSPclk <<'\n';
    digitalWrite(PWMSPrclk, HIGH);      /
    digitalWrite(PWMSPrclk, LOW);
    std::cout << "PWM SP R CLK"<< PWMSPrclk <<'\n';
    digitalWrite(SPclk, HIGH);      
    digitalWrite(SPclk, LOW);
    std::cout << "SP CLK"<< SPclk<<'\n';
    digitalWrite(SPrclk, HIGH);     
    digitalWrite(SPrclk, LOW);
    std::cout << "SP R CLK"<< SPrclk<<'\n';
}

}

"Motorcontrol01.h" :

#ifndef DEF_MotorControl
#define DEF_MotorControl
#include <vector>
class MotorControl{
    public:
        MotorControl();     //Constructeur
        void SendDataPWM(std::vector<std::vector<bool>> DataArr);

    private:
        std::vector<std::vector<bool>> DataArr;
        int mFrequency;
        int mGPIOOutputNb;
        int SPser, SPclk, SPrclk; //Define the output pins used for the main shift registers
        int PWMSPclk, PWMSPrclk;
};

When I compile it, I got some warnings saying that the variables "SPser, SPclk, SPrclk, PWMSPclk PWMSPrclk" are defined but not used...

and when I execute the program, it displays this :

I must have done a mistake somewhere, but I really don't have any idea where...

Or there is something I misunderstood in the tutorialS I read about the subject of classes and objects...

could someone highlight the obvious things I didn't get? :)

Thank's by advance for your help !


回答1:


You are declaring local variables in constructor and assign values to them. These variables have short live and theirs values are visible in constructor only (and are not related to member variables with the same names). Your constructor should look like this to assign values to the members which are used in other functions which use private variables of class (members).

MotorControl::MotorControl(){   //constructeur
    //  GPIO Pins definition
    SPser = 13;
    SPclk = 19;
    SPrclk = 26; //Define the output pins used for the main shift registers
    PWMSPclk = 17;
    PWMSPrclk = 18;  // define the output pins to control the PWM shift registers
}

Also it does not make sense to declare array in constructor because you do not use it there.



来源:https://stackoverflow.com/questions/60657246/methods-and-parameters-declaration-problem-in-a-class-with-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!