简体   繁体   中英

Arduino/C++: Access specific header file of structs from library

(Not sure whether this is exclusively a C/C++ issue)

I'm currently fragmenting elements of a large Arduino project into reusable libraries - so far soo good.

However, a number of methods in the libraries return special structs which are declared in a data-types.h file contained in each library. The problem I have now is I'm unable to import/utilise these structs in my main sketch. I've tried declaring a variable of the DataTypes class in the main library header file and accessing the structs through it, but I get error error: invalid use of 'struct DataTypes::_theStructNameHere_t'

How would I go about accessing these structs from the library in my main sketch to declare as a variable type? I don't want to have to copy the header file which contains the structs from the library into my sketch, and I also don't want to have to create a separate library just for this single header file of structs!

Here's a quick example of what I mean:

Main.cpp:

#include <Arduino.h>
#include <MyLibrary.h>

MyLibrary myLib;

void setup() {
    (This is declared in the library) myLib.dataTypes._theStructNameHere_t response = myLib.getASpecialValueWhichIsOfType_theStructNameHere_t()// Gives "error: invalid use of 'struct DataTypes::_theStructNameHere_t'""

    // Example usage of the struct:
    Serial.print("\n Loop Card Status: ");Serial.print(response.loop_status, HEX);
    if (response.number_allocated > 0) {
        Serial.print("\n Devices Allocated: ");Serial.print(response.number_allocated, HEX);
    } else {
        if (response.loop_status != 0x123) {
            // Some condition
        } else {
            // Something else
        }
    }
}

void loop() {
    ...
}

Library Structure:

    src/
    - /data-types/
    - - data-types.h
    - MyLibrary.cpp
    - MyLibrary.h 

Library Header MyLibrary.h :

#ifndef   _MYLIBRARY_H_
#define   _MYLIBRARY_H_

#include <Arduino.h>

#include "./helpers/helpers.h"
...
#include "./data-types/data-types.h"

class MyLibrary {

    public:
        Uart *_commPort;
        Helpers helpers;
        ... 
        DataTypes dataTypes;

        DataTypes::_theStructNameHere_t getASpecialValueWhichIsOfType_theStructNameHere_t();

      ...

    protected:
    private:

};

#endif // _MYLIBRARY_H_

DataTypes Class data-types.h :

#ifndef   _RESPONSE_TYPES_H
#define   _RESPONSE_TYPES_H

class DataTypes
{
    public:
      struct _theStructNameHere_t
        {
            bool successful;
            uint8_t loop_status;
            uint8_t number_allocated;
            uint8_t highest_address;
            uint8_t number_inputs;
            uint8_t number_outputs;
        }
        ..even more..
    private:
}
#endif // _RESPONSE_TYPES_H

I was able to obtain a MCVE from your example:

class DataTypes
{
    public:
    struct _theStructNameHere_t
    {

    };
};

class Library
{
    public:
        DataTypes dataTypes;
        DataTypes::_theStructNameHere_t getMyDataType();
};

int main(int argc, char *argv[])
{
    Library myLib;
    myLib.dataTypes._theStructNameHere_t response;
}

which gives a similar error as your code:

~$ g++ test.cpp 
test.cpp: In function 'int main(int, char**)':
test.cpp:20:21: error: invalid use of 'struct DataTypes::_theStructNameHere_t'
     myLib.dataTypes._theStructNameHere_t response;

The problem is that you use an instance to access the struct type/name. To fix it, replace

myLib.dataTypes._theStructNameHere_t response = ...;

with

DataTypes::_theStructNameHere_t response = ...;

Notes:

  • Instead of using classes to create separate namespaces, please consider using namespaces directly. This is a feature of C++ which is available under Arduino .
namespace Library {

namespace DataTypes {

struct _theStructNameHere_t
{
    ...
};

...

} /*** namespace Library::DataTypes ***/

} /*** namespace Library ***/
  • Please read StackOverflow guidelines concerning how to ask a good question , in particular the section about Mininimal, Complete and Verifiable Example .

  • Sooner or later someone will tell you that there is no such thing as C/C++ ; C is C and C++ is C++ ; Arduino lives in its own world, even if is based on C++ . Thus, you might want to remove C and C++ tags from your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM