从命名空间传递struct对象作为参数

时间:2017-03-22 21:48:11

标签: c++ oop

我在将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
    };
}

然后我有我的示例类:

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 ?????
}

如果有人可以帮助我,我很乐意。

1 个答案:

答案 0 :(得分:1)

您应该使用命名空间名称限定您的类型:

CMyclass(settings::TServoSettings * ptr); 
         ^^^^^^^^^^