-2

I have problem with passing struct object from specified namespace as parameter in constructor of class. I have my struct:

settings.h

namespace settings
{
    typedef struct
    {
        volatile uint8_t ddr;
        volatile uint8_t port;
        uint8_t pin1;
        uint8_t pin2;
        uint8_t timerChannel;
        uint8_t showChannel;
        uint8_t sensePin;
        uint16_t resistorValue;
        uint16_t maxCurrent;
    }TServoSettings;
}

settings.cpp

#include "settings.h"

namespace settings
{
     TServoSettings servo1 =
    {
            //Here are some values from preprocessor definies
            SERVO1_DDR,
            SERVO1_PORT,
            SERVO1_PIN1,
            SERVO1_PIN2,
            SERVO1_TIMER_CHANNEL,
            SERVO1_SHOW_CHANNEL,
            SERVO1_SENSE,
            SERVO1_R,
            SERVO1_MAXCURR
    };
}

And then I have my example class:

myclass.h

class CMyclass
{
public:
    CMyclass(TServoSettings * ptr); //<------HOW CAN I PASS HERE CREATED BEFORE OBJECT OF STRUCTURE FROM   NAMESPACE AS A PARAMETER - POINTER??????
    ~CMyclass();
};

myclass.cpp

#include "myclass.h"
CMyclass::CMyclass(TServoSettings * ptr) //HOW PASS ??
{
    //Do some things with this pointer
}

CMyclass::CMyclass()
{
    //Hothing to do here :(
}

main.cpp

#include "settings.h"
#include "myclass.h"

int main()
{
    CMyclass someobject(&settings::servo1);    //AND HOW PASS HERE ?????
}

I would like to be glad if someone can help me with this.

drewpol
  • 665
  • 1
  • 6
  • 26

1 Answers1

1

You should qualify your type with namespace name:

CMyclass(settings::TServoSettings * ptr); 
         ^^^^^^^^^^
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Yes, and I get an error : `error: 'settings' has not been declared` – drewpol Mar 22 '17 at 21:55
  • well, then add `#include "settings.h"` at the top of myclass.h. This error probably shows up only for myclass.cpp. – marcinj Mar 22 '17 at 21:57
  • 1
    also dont forget about include guards: in settings.h and myclass.h – marcinj Mar 22 '17 at 21:58
  • 1
    @drewpol And don't forget to learn some basic C++. Here's a [list of good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Mar 22 '17 at 22:04
  • Thanks for Your tips. I added include guards. I included also `#include "settings.h"` in myclass.h. Now in myclass files everythng looks good, but in main file I get error `error: 'servo1' is not a member of 'settings'`. I added includes in main file too. – drewpol Mar 22 '17 at 22:04
  • sorry for last comment, I have no idea whats wrong now. – marcinj Mar 22 '17 at 22:09
  • ok, now its clear to me, servo1 is in settings.cpp, so you have no access to it in main.cpp – marcinj Mar 22 '17 at 22:13
  • 1
    you may add `namespace settings { extern TServoSettings servo1; }` to main.cpp to fix this error. Or maybe to settings.h would be a better place. – marcinj Mar 22 '17 at 22:14