0

I am trying to assign a static pointer in a class so I can access it by class functions.

Basically, the typeDef is defined in a third party header file.

typedef struct
{
  CAN_TypeDef                 *Instance;  /*!< Register base address          */

  CAN_InitTypeDef             Init;       /*!< CAN required parameters        */

  CanTxMsgTypeDef*            pTxMsg;     /*!< Pointer to transmit structure  */

  CanRxMsgTypeDef*            pRxMsg;     /*!< Pointer to reception structure */

  __IO HAL_CAN_StateTypeDef   State;      /*!< CAN communication state        */

  HAL_LockTypeDef             Lock;       /*!< CAN locking object             */

  __IO uint32_t               ErrorCode;  /*!< CAN Error code                 */

}CAN_HandleTypeDef;

Here is my Test class header file "Test.hpp"

#pragma once
#include <stm32f4xx_hal.h>
class Test
{
public:
    static CAN_HandleTypeDef* hcan1;

};

Here is the Test class code "Test.cpp"

#include "Test.h"


void DoSomething()
{       
    HAL_CAN_Receive(Test::hcan1, CAN_FIFO0, 100);
}

Here is the main function where I try to assign it a value

int main(void)
{

  CAN_HandleTypeDef hcan1;
  <-- Code is here that uses and populates hcan1 -->
  // Initialize libraries
    Test::hcan1 = &hcan1;
}

When I try to compile I get the "error : undefined reference to `Test::hcan1'"

I am new to cpp, but I know when you declare you have to also define. However, when I try to define it tells me I have multiple declarations.

What's the solution here?

Justin
  • 24,288
  • 12
  • 92
  • 142
Matt H
  • 137
  • 1
  • 10
  • 1
    *"However, when I try to define it tells me I have multiple declarations."* - Then you made some mistake. Why do you show the code which you know cannot work (because there is no definition) but do not show the code which you've tried and which should work (because it has a definition)? – Christian Hackl Apr 21 '17 at 21:08
  • You need a Test.cpp where you can define your static variable, then compile and link this with main etc. – Paul R Apr 21 '17 at 21:08
  • Thanks all for the comments. I finally figured it out. Adding "CAN_HandleTypeDef *Test::hcan1;" to the cpp file fixed it. Still learning I guess – Matt H Apr 21 '17 at 21:48

0 Answers0