0
#include "MPU6050.h"

class DATA{
    public:
    DATA(int A);
    void Sensor();
    }


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

void Sensor(){
mpu.setFullScaleAccelRange(3);
// 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.

Thanks

Digol
  • 173
  • 10
  • 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
  • @Hadar.S I'm not a C++ programmer (I'm programming Java and Python), but it's pretty obvious that you haven't understood some of the most basic concepts of variable declaration. Are following some kind of tutorial to learn C++? Like this I would be able to point what section you need to re-read and go from there. – MadMike Feb 24 '17 at 15:37
  • No mike that is my code. Not tutorial. Sorry. I just ask how to use a variable/object created in a construcrot in other friend fintions of the same class. Appreciate your will to help ! – Digol Feb 24 '17 at 15:44
  • @Hadar.S I didn't mean to imply that this isn't your code. I wanted to know if you are using a a tutorial beside writing this code. Working with a tutorial would really help you at your stage of learning how to programm C++. The way you use stackexchange to learn to programm has allready angered people on other sites to the point that it scored you a lenghty ban. I'd take that as a hint to change the way of learning things. – MadMike Feb 24 '17 at 16:09
  • Sure I was learninh c++ for couple of monthes with tutorials but never encountered a tutorial that deal with this question. Also i searched online and here on Stack. – Digol Feb 24 '17 at 16:57
  • @Hadar.S point me to the tutorials you've read and I shall point you which parts to re-read. – MadMike Feb 24 '17 at 18:53
  • The tutorials are in Hebrew :). Can you point me to a tutorial that suits this question? – Digol Feb 24 '17 at 18:56

1 Answers1

4

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;
    public:
    DATA(int A) : mpu(A) {};
    void Sensor();
    }
Phil B.
  • 5,043
  • 15
  • 30
  • 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
  • @Hadar.S: don't confuse declaration (in the class section) with initialization (in the sensor() function). If you need a second MPU6050 variable, just add it in the class section after the first declaration. Then initialize it (i.e. MPU6050 mpu2(0x69);) in the place where you need it. – Phil B. Feb 24 '17 at 19:04
  • But I want variables with the same name to use with the sensor. Like mpu1.accelerometerx, mpu2.accelerometerx. I am using two MPU6050 IMUs – Digol Feb 24 '17 at 19:09
  • @PhilB. Phil's answer is the solution. Totally forgot that if I need to pass a value to mpu constructor I should do that inside DATA constructor. Thank you both for your help. Thank you Phil for this answer. (You might edit your answer with DATA::DATA(int a):mpu(a){} so I can mark it as an answer. – Digol Feb 25 '17 at 09:44
  • Hadar, this is what initialization lists are for in C++. I strongly recommend you get yourself a decent intro to C++ book (preferably one recent enough to include C++11) and work through it. It is a very complex language and going about learning it this way is not going to be very effective. Also, for future reference general programming questions belong on our larger parent site, Stack Overflow. – goldilocks Feb 25 '17 at 11:06
  • @Hadar.S: Added the base constructor call to the code sample as per our chat conversation. – Phil B. Feb 25 '17 at 12:03