#include "MPU6050.h"

class DATA{
    DATA(int A);
    void Sensor();

DATA::DATA(int A){
MPU6050 mpu(A);

void Sensor(){
// the problem is here, "mpu" object is not declared for this friend function

int main(){
DATA mpu1(0x68)       
/* I'm using two MPU6050 on I2C bus so I need to address them.
 this example contains only one MPU6050 object*/
    return 0;

Compiler Error : "mpu was not declared in this scope"

While writing this code I had a feeling that the mpu object is not declared for Sensor friend function. Is there a way to deal with this case?

I'm a C++ Beginner.


  • Is this the whole code? You haven't declared what an MPU object is anywhere. What is it and where did you get it from? – Jacobm001 Feb 24 '17 at 14:57
  • 1
    Sounds like a generic programming question which has little to nothing to do with Raspberry Pi. I think this question is better asked on StackOverflow and should be marked off-topic here. – MadMike Feb 24 '17 at 15:00
  • It's not the whole code, the code is too big. It's just a little example to present the situation. I edited the code with include due to your comment – Digol Feb 24 '17 at 15:01
  • @MadMike I have no idea why I have a Ban from StackOverflow. I'm writing this on Raspberry Pi using Geany so it would be great not to mark this question as off-topic. – Digol Feb 24 '17 at 15:03
  • @MadMike that is the whole code to compile. – Digol Feb 24 '17 at 15:16

I am a bit rusty on my C++ (it's been 15+ years) but I think the issue is that you didn't declare the mpu variable as a private (or protected) member of the class. Instead it only gets a declaration (and I'm not sure that's done in the right way, my (Java-influenced) brain says you should likely do MPU6050 mpu = mpu(A); to make the correct declaration) in the DATA constructor, which means its visibility is limited to the constructor, hence the Sensor() member function cannot access it.

Net, what you want to do is change the class declaration to:

class DATA{
    MPU6050 mpu;
    DATA(int A) : mpu(A) {};
    void Sensor();
  • I think it would be nice if you would include the whole code and extend it to a (somewhat) compilable example. (I'd also give +1 for that :) ) – MadMike Feb 24 '17 at 16:55
  • The MPU6050 is a class. "MPU6050 mpu(0x68)" That is the right way to create an object because it works in another code I wrote to test it. This object is not recognized by the friend function. Is there a way to initialize an object as global ? Just like writing "MPU6050 mpu(0x68)" right below the include line? – Digol Feb 24 '17 at 17:08
  • @MadMike there - added the code sample :) – Phil B. Feb 24 '17 at 17:25
  • +1 @PhilB. I'm unsure if it is necessary to be that close to the original indentation... :) – MadMike Feb 24 '17 at 18:48
  • Ok lets try to figure it out in that way. When I initialize an object to MPU6050 class, I need to pass the address. I wish to create two MPU6050 objects, once with 0x68 and once with 0x69. "MPU6050 mpu1(0x68)"&"MPU6050 mpu2(0x69)". With your solution it is not possible I think. Maybe I'm wrong? – Digol Feb 24 '17 at 18:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.