Pages: [1]   Go Down
Author Topic: What's wrong with this c++ code?  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am converting the Xbee library to use both Serial and NewSoftSerial, so it can be used in conjunction with USB access to the mcu. But I'm not sure why I'm getting this error:

softxbee.cpp: In member function 'void softxbee::begin(long int)':
softxbee.cpp:23: error: '((softxbee*)this)->softxbee::Nss' does not have class type

.h:
Code:
#ifndef softxbee_h
#define softxbee_h

#include <WProgram.h>
#include <inttypes.h>
#include <../NewSoftSerial/NewSoftSerial.h>

class softxbee {
public:
      softxbee();
      softxbee(uint8_t, uint8_t);

      uint8_t _rxpin, _txpin;
      boolean _usingsoftserial;
      NewSoftSerial& Nss(uint8_t, uint8_t);
      void begin(long baud);
};

#endif //softxbee _h

.cpp:
Code:
#include "softxbee.h"
#include "WProgram.h"
#include "HardwareSerial.h"
#include <../NewSoftSerial/NewSoftSerial.h>


softxbee::softxbee(){
      _usingsoftserial = false;
}

softxbee::softxbee(uint8_t rxpin, uint8_t txpin){
      softxbee();
      _usingsoftserial = true;
      _rxpin = rxpin;
      _txpin = txpin;
      Nss(rxpin, txpin);
}

void softxbee::begin(long baud) {
      if (!_usingsoftserial)
            Serial.begin(baud);
      else
            Nss.begin(baud);
}

My sketch to test this:
Code:
#include      <softxbee.h>
#include      <NewSoftSerial.h>

softxbee xbee;                                                // coordinator
 
void setup()
{
Serial.begin(9600);
xbee = softxbee(8,9);
xbee.begin(9600);
}

void loop()
{
}
« Last Edit: April 09, 2010, 12:57:35 pm by maxmike » Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1. You don't have to re-include headers or such everywhere, just do it once in your library's header or cpp file (for WProgram.h, inttypes.h, NewSoftSerial.h, etc); when you include your header, the others will be included automatically.

2. Your problem appears to be that the compiler can't find the NewSoftSerial library; I think it may have to do with your relative link - have you tried just including "NewSoftSerial.h", without any path information (relative or otherwise)?

I recently wrote a library that I am still testing, and I included NewSoftSerial; but I don't remember how I did it exactly and it isn't in front of me - if none of the above works, and you can wait (and you don't get any other answers from others that help), I'll try to remember to post here about it again (if not, PM me).

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, Cr0sh - changing the include doesn't affect the problem.
I'm just trying to instantiate a NewSoftSerial object when the softxbee constructor is called.Maybe you could find a similar line in your code.
Will try some more. Found:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1211645129

but his solution doesn't seem to help me here.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Writing "NewSoftSerial& Nss(uint8_t, uint8_t);" declares a new method called Nss, not an object. Just write "NewSoftSerial& Nss;" to declare an object.

To initialize it you are supposed to use an initialization list in the constructor. I've never had to do this on an Arduino yet so I don't know if it works but this is what it looks like:

Code:
softxbee::softxbee(uint8_t rxpin, uint8_t txpin)
: Nss(rxpin, txpin)  // this is an initializer
{
      softxbee();
      _usingsoftserial = true;
      _rxpin = rxpin;
      _txpin = txpin;
}
Logged


0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to both of you! The problem was that the default constructor also needed an initialization for Nss  --> softxbee::softxbee() : Nss(2,1)
so I gave it pins 1 and 2 as a default.

My own problem is that I've been doing Java for so long that stepping back into the medieval world of C++ with all its contrived artifacts is a serious drag for me. However, I do recognize we'd need at least a 100MHz Arduino to support Java.
« Last Edit: April 09, 2010, 08:02:28 pm by maxmike » Logged

Pages: [1]   Go Up
Jump to: