Problem using classes

Hey I wante dto start programming objectoriented with my Arduino, but for some reason it is not working. I am getting “main:17: error: ‘Sd’ was not declared in this scope” with Sd being the name of the class (I am calling a static function). And also when I am trying to make an instance of the class I am getting the same thing.

main file

#include <Wire.h>
#include <SD.h>

#define GYRO_ADDRESS 0x6B
#define I2C_EXPIRED 100
#define SD_CS_PIN

void setup()
{
   Serial.begin(9600);
   //Sd.init(SD_CS_PIN);
   Sd sd1 = new Sd("classSD");
   //initGyro(0, 0);
}

void loop()
{
  
}

class file

class Sd
{
  private:
      String fileName;
      byte static csPin;
  
  public:
     Sd(String nameBeginning)
     {
          int i=1;
          while(SD.exists(nameBeginning+i+".txt")==true)
              i++;
          fileName = fileName + i+".txt";

     }
     
     static byte init(byte csPin2)
     {
          if(!SD.begin(csPin2))
          {
              Serial.println("Fehler beim Initialisieren des SD");
              return 1;
          }
          csPin = csPin2;
          return 0;
     }

     void writeLog(String text)
     {
          File myLOGfile = SD.open(fileName, FILE_WRITE);
          myLOGfile.println(text);
          myLOGfile.close();
     }
};

I thank you for your support in advane (=

I don't see you call a static method....

But the problem Sd != SD :wink: Which you/use would have noticed right away from the error if you would have copied it all instead of typing part of it... While the rest of the error might be magic for you, it contains more clues.

And drop the stupid String...

And

nameBeginning+i+".txt"

is not C/C++...

septillion:
I don't see you call a static method....

But the problem Sd != SD :wink: Which you/use would have noticed right away from the error if you would have copied it all instead of typing part of it... While the rest of the error might be magic for you, it contains more clues.

And drop the stupid String...

And

nameBeginning+i+".txt"

is not C/C++...

I commented that line before posting to see if it would create the instance.

I have no idea what you mean. I did post the whole error code. Sd is obviously not SD. Sd is my class and SD is the class natice to arduino.

I have no idea what "stupid string" you want me to drop and last the code you quoted works.

Because when I compile I don't get "main:17: error: 'Sd' was not declared in this scope". I get "sketch_dec06a.ino:11:17: error: 'SD' was not declared in this scope"...

Nooo, not stupid string, a string is fine. Stupid String, with a Capital :wink: Bulky, error prone and likes to make your memory into Swiss cheese if you don't know the pitfalls of it. And it's right here

  private:
      String fileName;

The point is you are including SD.h, not Sd.h

Or more exactly,
use

#include "Sd.h"

not

#include <SD.h>

So you search in your libaries, for your class, not the system libraries for the system class.

septillion:
Because when I compile I don’t get “main:17: error: ‘Sd’ was not declared in this scope”. I get “sketch_dec06a.ino:11:17: error: ‘SD’ was not declared in this scope”…

Nooo, not stupid string, a string is fine. Stupid String, with a Capital :wink: Bulky, error prone and likes to make your memory into Swiss cheese if you don’t know the pitfalls of it. And it’s right here

  private:

String fileName;

That is because you don’t have the two files in one project and therefore the class file has no #include<SD.h>

What is different between String and string? Coming from java I always use String

So that shows the class name you use is pore. Make it something more unique. And read up on creating libraries if you start messing with .h files :slight_smile:

MarkGoingToSpace:
What is different between String and string? Coming from java I always use String

Have q quick Google :wink:

MarkGoingToSpace:
Coming from java I always use String

Can see that (the part of not being C/C++). But that's just a difference between C/C++ and Java and it running on a tiny micro. Java runs on a more powerful device (and still is slow... ::slight_smile: Sorry, no Java fan here 8) )

MarkT:
The point is you are including SD.h, not Sd.h

Or more exactly,
use

#include "Sd.h"

not

#include <SD.h>

So you search in your libaries, for your class, not the system libraries for the system class.

again SD is for the sd class native to arduino. I don’t think I have to include the class, do I? because I know that I can use a function from a different file without including anything as long as the 2 files are in the same project.

Furthermore i tried and got the exact same error

I think the reason String is frowned upon is the same reason you don’t see a lot of malloc-ing in Arduino code.

MarkGoingToSpace:
again SD is for the sd class native to arduino. I don’t think I have to include the class, do I?

Look at your original posting - you included it! We’re telling you not to.

The difference between <> and “” in includes is about where to look for the file, <> means “system
places”, “” means “my stuff”, loosely speaking. IIRC it affects the order of search but not the list of
places searched in the Arduino setup, but you should know the difference anyway and use the right
version.

You haven’t shown us your Sd.h file yet have you?

Perhaps your file-system is a dreaded case-folded one, in which case Sd and SD will both
match any of Sd SD sd or sD. Choose a better class name to check that out, rather than risk
confusing name clashes. Life is too short for name clash nonsense!

gfvalvo:
I think the reason String is frowned upon is the same reason you don’t see a lot of malloc-ing in Arduino code.

The main reason is that it used to be very broken. And whether it was broken I believe also depended
on the exact version of the AVR-GCC compiler.

I believe it is safe to use these days, but the RAM penalty is not inconsiderable.

Yep, because it's realllll easy to make your memory into Swiss cheese and break the code if you don't know all about it. And then seems making a class for beginners who don't know anything about that doesn't seem so smart anymore...

MarkGoingToSpace:
because I know that I can use a function from a different file without including anything as long as the 2 files are in the same project.

And don't have C or C++ style filenames....

#include <SD.h>

This includes SD.h from the Arduino library directories, not your SD.h in your project directory. The Adruino SD library - evidently - does not define a class named ‘Sd’. To include your SD.h, it needs to be

#include "SD.h"

I’d suggest changing the name of the file to “MG2S_SD.h” or something so that the file names don’t collide. This would have made the problem a little more obvious.