【问题标题】:Methods and parameters declaration problem in a class with header带头的类中的方法和参数声明问题
【发布时间】:2020-03-12 15:30:53
【问题描述】:

大家好,

我在头文件中定义的类中声明某些参数时遇到了很大问题

我正在使用带有 C++ 的树莓派,并尝试通过一个类来实现一些对象。

因此,我有:

  • 我在其中调用对象的主 cpp 文件(它有效,我确信,你会看到)

  • 名为“Motorcontrol01.cpp”的类 cpp 文件

  • 名为“Motorcontrol01.h”的头文件

我的代码比我在这里展示的更复杂,但我出于测试目的对其进行了简化,但它无论如何都不起作用 -_- :

“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;
};

当我编译它时,我收到一些警告说变量“SPser、SPclk、SPrclk、PWMSPclk PWMSPrclk”已定义但未使用...

当我执行程序时,它会显示:

我一定是在某个地方做错了,但我真的不知道在哪里......

或者在我读到的关于类和对象的主题的教程中我误解了一些东西......

有人可以突出我没有得到的明显的东西吗? :)

提前感谢您的帮助!

【问题讨论】:

  • 请不要将文字作为图片发布。复制和粘贴会做得更好。此外,C++ 与 C 非常不同。请选择正确的标签
  • @Gerhardh 我无法复制命令提示符内容,因为它不停地流动(而且我不知道如何停止它......我是 linux 的新手) .我不是把它标记为 c++ 吗???我知道 C 和 C++ 不要混淆,如果我做错了对不起:) 可能是我的键盘的数字键盘无法通过我设置的 VNC 服务器被 Rpi 识别^^,

标签: c++ class object raspberry-pi


【解决方案1】:

您在构造函数中声明局部变量并为其赋值。这些变量的寿命很短,并且它们的值仅在构造函数中可见(并且与具有相同名称的成员变量无关)。 您的构造函数应该如下所示,以将值分配给其他函数中使用的成员,这些函数使用类(成员)的私有变量。

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
}

在构造函数中声明数组也是没有意义的,因为你没有在那里使用它。

【讨论】:

  • @maxim_sagaydachny 耶哈,就是这样!我知道这是非常简单的事情!非常感谢 !我的东西还是不行,但至少不是因为这个XD
猜你喜欢
  • 2013-08-22
  • 1970-01-01
  • 2016-12-13
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2012-12-12
  • 1970-01-01
相关资源
最近更新 更多