[英]C++ Method Definition & Variable Declaration
我試圖通過創建具有 .h 和 .cpp 文件的類,將我的 arduino 代碼庫從單個“ino”文件拆分為一個適當的 C++ 程序。 我遇到了一些我無法解決的錯誤。 我希望我錯過了一些簡單的東西。
在運行“Arduino IDE for Visual Studio”插件的 Visual Studio 中編譯時,出現以下錯誤:
7:8:錯誤:“類 MPU6050”沒有名為“計時器”的成員
30:5:錯誤:“int MPU6050::MPU6050_read(int, uint8_t*, int)”的原型與“MPU6050”類中的任何內容都不匹配
我在這里缺少什么?
。H
#ifndef MPU6050_H
#define MPU6050_H
class MPU6050
{
private: // Vars
uint32_t timer; // Hold the value of the timer used for the complementary filter
int error;
public: // Methods
MPU6050();
~MPU6050();
void read_acc_gyr();
float const * const getXa();
float const * const getYa();
float const * const getXvel();
float const * const getYvel();
float const * const getZvel();
float const * const getZang();
double const * const getDt();
private:
void test_gyr_acc();
void MPU6050_init();
void printGyroValues();
void calibrateGyro();
int MPU6050_write(int start, const uint8_t *pData, int size);
int MPU6050_read(int start, uint8_t *buffer, int size);
int MPU6050_write_reg(int reg, uint8_t data);
};
#endif
.cpp
#include "MPU6050.h"
#include "Arduino.h"
#include "MPU6050Definitions.h"
MPU6050::MPU6050()
{
timer = 0; // Always start the timer at 0
error = 1;
Serial.println("Testing Gyro");
test_gyr_acc();
}
MPU6050::~MPU6050()
{
}
int MPU6050::MPU6050_read(int start, uint8_t *buffer, int size)
{
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
// Third parameter is true: relase I2C-bus after data is read.
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while (Wire.available() && i<size)
{
buffer[i++] = Wire.read();
}
if (i != size)
return (-11);
return (0); // return : no error
}
void MPU6050::test_gyr_acc()
{
uint8_t c = 0;
error = MPU6050_read(MPU6050_WHO_AM_I, &c, 1);
if (error != 0) {
while (true) {
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);
}
}
}
MPUT6050Definitions.h 包含我的所有常量,如下所示:(還有更多我認為與包含無關的 #defines)
#pragma once
//All the values to setup the MPU6050 gyro/accelermeter
#define MPU6050_AUX_VDDIO 0x01 // R/W
#define MPU6050_SMPLRT_DIV 0x19 // R/W
#define MPU6050_CONFIG 0x1A // R/W
#define MPU6050_GYRO_CONFIG 0x1B // R/W
看起來我犯了一個簡單的錯誤。
列表中的更多錯誤是一個unknown declaration of type uint_32
它會自我傳播,導致我之前看到的錯誤。 我包含了Arduino.h
頭文件,它解決了uint_32
問題,導致其余代碼按預期編譯。
這個故事的寓意是不要試圖從上到下修復你的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.