Ahmad Banshee
Ahmad Banshee

Reputation: 33

problem with compiling C project in a C++ unit test

I have a struct forward declaration like below in C I added googletest and want to test my code using I added extern "C" in my test.cpp file but I get the error

baseCommand.h file

typedef struct Command_t Command_t;

struct Command_t{

    uint8_t id;
    uint8_t procId;
    uint8_t priority;
    char initCommand[50];
    char command[50]; 
    char commandParam[450];
    char finishParam[10];
    int32_t (*fpInit)(struct Command_t* this);
    uint16_t initDelayMs;
    int32_t (*fpSend)(struct Command_t* this);
    uint16_t sendDelayMs;
    int32_t (*fpReceive)(struct Command_t* this);
    char expectedAnswerOnSucessCommand[100];
    char expectedAnswerOnError[100];
    uint16_t receiveDelayMs;
    int32_t (*fpProc)(struct Command_t* this);
    int8_t retry;
    void (*fpReset)(void);
    int port;
    int32_t (*fpCtor)(struct Command_t* this);
};

test.cpp file

extern "C" {

#include "commands/baseCommand.h"


}

# include "gtest/gtest.h"


TEST(IntegerFunctionTest, negative) {
    
        EXPECT_EQ(1, 1);

}

but I got the error (Updated I deleted some not related files )

expected ',' or '...' before 'this' baseCommand.h

on line

int32_t (fpInit)(struct Command_t this);

Upvotes: 0

Views: 175

Answers (2)

Nishad C M
Nishad C M

Reputation: 174

Please check below code, for q, "I am forward decalring Command_t to use its pointer inside itself - ah ban 28 mins ago"

    typedef struct Command_t * Command_tp;

    struct Command_t {

        uint8_t id;
        uint8_t procId;
        uint8_t priority;
        char initCommand[50];
        char command[50];
        char commandParam[450];
        char finishParam[10];
        //int32_t (*fpInit)(struct Command_t* this);
        int32_t (*fpInit)(Command_tp);
        uint16_t initDelayMs;
        //int32_t (*fpSend)(struct Command_t* this);
        int32_t (*fpSend)(Command_tp);
        uint16_t sendDelayMs;
        //int32_t (*fpReceive)(struct Command_t* this);
        int32_t (*fpReceive)(Command_tp);
        char expectedAnswerOnSucessCommand[100];
        char expectedAnswerOnError[100];
        uint16_t receiveDelayMs;
        //int32_t (*fpProc)(struct Command_t* this);
        int32_t (*fpProc)(Command_tp);
        int8_t retry;
        void (*fpReset)(void);
        int port;
        //int32_t (*fpCtor)(struct Command_t* this);
        int32_t (*fpCtor)(Command_tp);
    };

Upvotes: 1

dbush
dbush

Reputation: 223739

Here:

int32_t (*fpInit)(struct Command_t* this);

You're getting an error because this is a keyword in C++ and therefore can't be used as an identifier. You need to change the name to something else.

Upvotes: 4

Related Questions