Arduino C ++中对类函数的未定义引用

时间:2015-12-26 05:31:23

标签: c++ arduino

我是Arduino的新手,最近我买了几台VEX机器人3线电机,我正在尝试用它来构建一个简单的机器人。我正在为此编写一个库,并编写一个简单的代码来测试它。当我运行此代码时,我收到错误:

Arduino: 1.6.3 (Mac OS X), Board: "Arduino Uno"

RobotTestDrive.cpp.o: In function `setup':
/Users/pawlak/RobotTestDrive.ino:5: undefined reference to `RobotVEXMotor::RobotVEXMotor(int)'
/Users/pawlak/RobotTestDrive.ino:6: undefined reference to `RobotVEXMotor::RobotVEXMotor(int)'
/Users/pawlak/RobotTestDrive.ino:8: undefined reference to `RobotVEXMotor::SetInvert(bool)'
/Users/pawlak/RobotTestDrive.ino:10: undefined reference to `RobotVEXMotor::SetPower(long)'
/Users/pawlak/RobotTestDrive.ino:11: undefined reference to `RobotVEXMotor::SetPower(long)'
collect2: error: ld returned 1 exit status
Error compiling.

这是我正在使用的程序:

#include </Users/pawlak/Documents/Arduino/libraries/VEX_Robot_Control/VEX_Robot_Control.h>

void setup() 
{
  RobotVEXMotor LeftMotor(11);
  RobotVEXMotor RightMotor(3);

  LeftMotor.SetInvert(true);

  LeftMotor.SetPower(1);
RightMotor.SetPower(1);
}

void loop() 
{
}

这是.h文件:

#ifndef VEX_Robot_Control_h
#define VEX_Robot_Control_h

#include "Arduino.h"

class RobotVEXMotor
{
    public:
        RobotVEXMotor(int pin);
        void SetPower(long power);
        void SetInvert(bool yn);
    private:
        int m_pin;
        int m_yn;
};

#endif

这是.cpp文件:

#include "Arduino.h"
#include "VEX_Robot_Control.h"


RobotVEXMotor::RobotVEXMotor(int pin)
{
    pinMode(pin, OUTPUT);
    m_pin = pin;
}

void RobotVEXMotor::SetPower(float power)
{
    analogWrite(pin, int((m_yn * power * 126) + 127));
}

void RobotVEXMotor::SetInvert(bool yn) //since motors are mirror to each other
{
    if(yn = true)
    {
        m_yn = -1;
    }
    else
    {
        m_yn = 1;
    }
}

据我所知,这是过去出现的话题;但是,我似乎无法按照不同线程上的说明修复问题。

1 个答案:

答案 0 :(得分:-1)

#include - 您的资源中的.h图书馆((SELECT COUNT(*) FROM `PlayerDetails` WHERE PlayerID = $PlayerID AND WL='...' ))已经不够了;您还需要告诉链接器在您的可执行文件中包含二进制代码。每https://www.arduino.cc/en/Reference/Libraries

  

要在草图中使用库,请从草图&gt;中选择它。导入库。