Functions inside a library

Hey guys, I'm writing a library for my gamepad project. I'm confused on how i would include a function into my .cpp file. For instance i want to create a void shift() function, and be able to call upon it within the library it's self.

game::updateScreen(){
shift(gdata);
shift(rdata);
}

void shift(byte inbyte){
//do somthing with that byte
}

That's an example of what i want, but it keeps on saying shift() was not declared in this scope, even though i included it in the header file. Thanks Dan

Can you show a complete example of this behaviour? Both .cpp and .h file?

I think you will need 'extern' here.

For example, assume there is a function void shift(byte inbyte) defined in Foo.cpp. To be able to use it, you'll need to include Foo.h and also have this code on top:

extern void foo(byte);

I'm not sure if you must put 'extern' keyword or not. It may be a default behavior to look for it in other included files. But I suggest you to put that so you won't waste time looking for shift function's body if you forget where it was implemented.

That’s an example of what i want, but it keeps on saying shift() was not declared in this scope, even though i included it in the header file.

If it is part of the class…

void game::shift(byte inbyte){
//do somthing with that byte
}

Sorry i didn’t post earlier this has been a busy week for me.
Here’s the header file

#ifndef game_h
#define game_h

#include "WProgram.h"

class game
{
	public:
		game (int data, int clock, int latch);
		void writeGworld(int gx, int gy, int gstate);
		void writeRworld(int rx, int ry, int rstate);
		void updateScreen();
		byte readcontrols();
	private:
		byte cdata;
		byte gworld1[16];
		byte gworld2[16];
		byte rworld1[16];
		byte rworld2[16];
		void writedata(int i, byte Gdata1, byte Gdata2, byte Rdata1, byte Rdata2);
		void shift(byte bytein);
		int _gx;
		int _gy;
		int _rx;
		int _ry;
		int _data;
		int _clock;
		int _latch;
};

#endif

and the .cpp

#include "WProgram.h"
#include "game.h"

game::game(int data, int clock, int latch)
{
	_data = data;
	_clock = clock;
	_latch = latch;
	DDRD = DDRD | B11101100;
}


void game::writeGworld(int _gx, int _gy, int gstate)
{
	if(_gx <= 8)
	{
		if(gstate == 1)
		{
			gworld1[16-_gy] = gworld1[16-_gy] | 1<<(8-_gx);
		} else {
			gworld1[16-_gy] = gworld1[16-_gy] ^ 1<<(8-_gx);
		}
	} else {
		if(gstate == 1)
		{
			gworld2[16-_gy] = gworld2[16-_gy] | 1<<(8-_gx);
		} else {
			gworld2[16-_gy] = gworld2[16-_gy] ^ 1<<(8-_gx);
		}
	}

}


void game::writeRworld(int _rx, int _ry, int rstate)
{
	if(_rx <= 8)
	{
		if(rstate == HIGH)
		{
			rworld1[16-_ry] = rworld1[16-_ry] | 1<<(8-_rx);
		} else {
			rworld1[16-_ry] = rworld1[16-_ry] ^ 1<<(8-_rx);
		}
	} else {
		if(rstate == HIGH)
		{
			rworld2[16-_ry] = rworld2[16-_ry] | 1<<(8-_rx);
		} else {
			rworld2[16-_ry] = rworld2[16-_ry] ^ 1<<(8-_rx);
		}
	}
}


void game::updateScreen()
{
	for(int i=0;i<16;i++)
	{
		byte Gdata1 = gworld1[i];
		byte Gdata2 = gworld2[i];
		byte Rdata1 = rworld2[i];
		byte Rdata2 = rworld2[i];
		Gdata1 = Gdata1 ^ B11111111;
		Gdata2 = Gdata2 ^ B11111111;
		Rdata1 = Rdata1 ^ B11111111;
		Rdata2 = Rdata2 ^ B11111111;
		writedata(i,Gdata1,Gdata2,Rdata1,Rdata2);
	}
}


void game::writedata(int i, byte Gdata1, byte Gdata2, byte Rdata1, byte Rdata2)
{
	PORTD = B00000000;
      shift(Rdata2);
      shift(Rdata1);
      shift(Gdata2);
      shift(Gdata1);
      shift(1<<i);
	shift(1<<i);
      PORTD = B00100000;
}


void game::shift(byte bytein)
{
	for(int b=0;b<8;b++)
	{
		PORTD = B00000000;
			if(bytein & 1<<b)
			{
				PORTD = B10000000;
			} else {
				PORTD = B00000000;
			}
		PORTD = PORTD | B01000000;
	}
}
		

byte game::readcontrols()
{
  cdata = B00000000;
  digitalWrite(_latch, LOW);
  digitalWrite(_clock, LOW);
  digitalWrite(_latch, HIGH);
  delayMicroseconds(150);
  digitalWrite(_latch, LOW);
  if(digitalRead(_data) == 0)
  {
    cdata = cdata | 1<<0;
  }
  for(int i=1;i<=7;i++){
    digitalWrite(_clock, HIGH);
    delayMicroseconds(150);
    if(digitalRead(_data) == 0)
    {
      cdata = cdata | 1<<i;
    }
    digitalWrite(_clock, LOW);
  }
return cdata;
}

That compiled OK for me.

Yes, i got it to compile correctly, but for some reason it still doesn't work correctly. I can send the writeGworld() variables, but i had it print the values it was receiving, and it returns just 1, and 0. Any idea on why the writeGworld command might not be receiving the correct values.

I can send the writeGworld() variables, but i had it print the values it was receiving, and it returns just 1, and 0. Any idea on why the writeGworld command might not be receiving the correct values.

There could be any number of reasons. Serial.print() statements before the call would confirm that the data was correct before the call. Showing us some code that calls the function is really necessary, though.