2
\$\begingroup\$

I want to inherit my own class from LiquidCrystal that creates some custom characters during initialization. Here's the code I've written so far:

MyProject.ino

#include <LiquidCrystal.h>
#include "MyLiquidCrystal.h"

MyLiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
 lcd.begin(16, 2);
 lcd.setCursor(0,0);
 lcd.print("Welcome");
}

void loop()
{
}

MyLiquidCrystal.h

#include <LiquidCrystal.h>
#include <Arduino.h>

namespace MyProject
{
    class MyLiquidCrystal: public LiquidCrystal
    {
        private:               
            void createCustomChars()
            {
                byte smiley[8] = { B00000, B10001, B00000, B00000, B10001, B01110, B00000 };                    
                this.createChar(0, smiley);
            }

        public:
            MyLiquidCrystal(uint8_t rs,  uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) 
                : LiquidCrystal(rs, enable, d0, d1, d2, d3)
            {
                createCustomChars();
            }
    };
};

I'm getting the following error at line MyLiquidCrystal lcd(8, 9, 4, 5, 6, 7);:

MyProject:5: error: 'MyLiquidCrystal' does not name a type

I've been googling for a while now but couldn't figure out what's going on. Any help would be appreciated!

\$\endgroup\$
  • \$\begingroup\$ It's not what you asked, but if the Arduino language is anything like C++, you should only have method declarations, not method definitions, in a .h file. \$\endgroup\$ – The Photon Jun 16 '13 at 14:14
  • \$\begingroup\$ Why would you use this namespace stuff for embedded? How many dozens of developers are working on this? How many megabytes is the executable? \$\endgroup\$ – Kaz Jun 17 '13 at 2:53
  • \$\begingroup\$ I saw the using of namespaces in a tutorial. I know what are they good for (I'm a C# developer). Only embedded development is new to me. \$\endgroup\$ – papaiatis Jun 18 '13 at 11:42
1
\$\begingroup\$

Try removing namespace MyProject from the header file. That's not required and means that it's not finding the class located outside the namespace. Alternatively you can use the new namespace prefix in your main project as another answer suggests.

\$\endgroup\$
  • 1
    \$\begingroup\$ Alternatively, in MyProject, change your type reference to MyProject::MyLiquidCrystal, or add a line using namespace MyProject;, but namespaces for Arduino projects are likely to be overkill. \$\endgroup\$ – microtherion Jun 16 '13 at 13:13
  • 2
    \$\begingroup\$ In that case, using a namespace is overkill indeed. However, it is untrue that using namespace in header is bad practice. That's exactly how they should be used (for project that warrants their use). \$\endgroup\$ – abey Jun 16 '13 at 13:14
  • \$\begingroup\$ -1 for stating that a namespace mentioned in a header is bad practice! \$\endgroup\$ – Wouter van Ooijen Jun 16 '13 at 17:29
  • \$\begingroup\$ @WoutervanOoijen, true it was getting late... Have revised it. \$\endgroup\$ – PeterJ Jun 17 '13 at 1:23
6
\$\begingroup\$

Since your class MyLiquidCrystal is declared inside a namespace (MyProject), you need to use the full type name to use it outside of that namespace. In your case, you should replace:

MyLiquidCrystal lcd(8, 9, 4, 5, 6, 7);

by

MyProject::MyLiquidCrystal lcd(8, 9, 4, 5, 6, 7);

Alternatively, you could add the following line at the top of your sketch:

using namespace MyProject;

This will tell the compiler that the whole content of MyProject namespace should be imported to the global namespace, which will allow direct access to its content.

\$\endgroup\$
  • \$\begingroup\$ This is also a solution indeed! However, PeterJ was faster thus the points go for him. Thank you for your help too! :) \$\endgroup\$ – papaiatis Jun 16 '13 at 16:21
  • \$\begingroup\$ Feel free to move the "accept" to whichever you ultimately judge is the better answer... \$\endgroup\$ – Brian Drummond Jun 16 '13 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.