Go Down

Topic: Error creating library: 'SoftwareSerial' does not name a type (Read 5514 times) previous topic - next topic

CyberFox

Jul 18, 2012, 10:29 am Last Edit: Jul 18, 2012, 10:56 am by CyberFox Reason: 1
hi there everybody
i'm new to the Arduino world and I think i am missing some basical stuff to make things working well..
I used to code a lot in c/c++,java etc, now in Arduino I have some problems with the language and the coding standards..

I am trying to write a simple library for an Arduino UNO using Arduino  IDE 1.0.1 on windows 7 x64.

I wrote these files:

serial2.h
Code: [Select]
#ifndef serial2_h
#define serial2_h
#include <Arduino.h>

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

#endif



serial2.cpp
Code: [Select]
#include "Arduino.h"
#include "serial2.h"
#include <SoftwareSerial.h>

SoftwareSerial Serial_2(2, 3);

serial2::serial2(){
 
}


Uno.ino
Code: [Select]
#include <serial2.h>

void setup(){
}

void loop(){
}


but I am getting this error:

Code: [Select]
serial2.cpp:4: error: 'SoftwareSerial' does not name a type

I used SoftwareSerial in another skech and it worked well, but i cannot figure out why in this library is not recognized.
Am I writing the library in a wrong way?
Thanks everybody

PaulS

You are trying to hide the use of a library from the sketch. You can not do that. You must do what everyone else does, and tell the sketch that you are using SoftwareSerial, but #including it's header file.

CyberFox

thanks for the reply, so  i have to specifically include the library also in the main file?
I modified the files in this way, but i'm still getting the same error

serial2.h
Code: [Select]
#ifndef serial2_h
#define serial2_h
#include <Arduino.h>


class serial2
{
public:
     serial2();
private:
      SoftwareSerial Serial_2(2, 3);
};

#endif


serial2.cpp
Code: [Select]
#include "Arduino.h"
#include "serial2.h"


serial2::serial2(){
}


Uno.ino
Code: [Select]
#include <serial2.h>

#include <SoftwareSerial.h>

void setup(){
}

void loop(){
}

PaulS

Your header file references the SoftwareSerial class. You need to #include SoftwareSerial.h in your header file, not in your source file.

Then. you will have other errors. The private member Serial_2 is defined AND INITIALIZED in the header. That is not allowed. You need to define it, only:

Code: [Select]
      SoftwareSerial Serial_2;

Then, in the source file, you need to initialize it:
Code: [Select]
serial2::serial2():Serial_2(2,3)

Go Up