This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Why can't you add a new class in Segger ?

What hidden settings am I missing this time in the awful Segger studio ?

I get an error :

unknown type name class

When I compile with this new class called Test.cpp :

.cpp file:

#include "Test.h"


Test::Test()
{
    
} 

and the .h file :

#include <stdlib.h>
#include <stdio.h>

    class Test
    {
        
        
    public:
        
        Test();
     
    
        
    private:
        
         
    
    };

EDIT:

Not sure why people are down voting but I think it's a real problem, at least for a beginner .

if I change the main to main.cpp I get 250 errors, some of them are :

'->' cannot appear in a constant-expression
'&' cannot appear in a constant-expression
a cast to a type other than an integral or enumeration type cannot appear in a constant-expression

They are coming from C files used by Nordic (drivers and libraries). The project was an example project from SDK, and I'v added other things to it.

I asked some people about it on the net, and they said I can't just mixed C and C++ files, so for example:

//main.cpp
#include "flash.h"
int main(void){}

Will produce errors because I include C file in C++ file , where the C might not be properly writen for C++.

Some of the errors comes from lines like this :

NRF_GPIOTE_TASKS_OUT_0     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
  • Hi, if I change the main to cpp I get 250 errors, some of them are :

    '->' cannot appear in a constant-expression
    '&' cannot appear in a constant-expression
    a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
    

    They are coming from C files used by Nordic (drivers and libraries). The project was an example project from SDK, and I'v added other things to it.

    I asked some people about it on the net, and they said I can't just mixed C and C++ files, so for example:

    main.cpp
    #include "flash.h"
    int main(void){}
    

    Will produce errors because I include C file in C++ file , where the C might not be properly writen for C++.

    Some of the errors comes from lines like this :

    NRF_GPIOTE_TASKS_OUT_0     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
    
  • Yes, if you have C code in the main.c file, you cannot simply change it to .cpp. See for instance this page for more information about calling C++ functions from C.

  • Thanks a lot, I have actually seen this page and tried it all, nothing helps. My question is, if you want to write a complex embedded application, you probably would like to use C++ to make it more powerful( arn't you? ) and you will always have C code from the drivers, and the libraries. So at some point you would like to include a c++ file to your code. To do that I did as you suggested, changed main to be cpp , so I can include cpp. Then everyone says you can't do that. So what CAN I do to use C++ ? :)

  • Our SDK does not support C++ beyond the header guards in the drivers/libraries, and as far as I know this will not change in the near future. If you want to use C++, you need to spend the time it takes to convert the project into C++. There are much resources online that can help you solve any issues you could encounter. This is not a Nordic-specific issue, you would have encountered the same issues converting any C project into supporting C++.

  • Ok thanks, sorry for asking. I just keep wondering if writing a production code only in C is something people usually do.

Related