"undefined reference" errors, trying to make a library

I'm trying to make a library and have run into a problem with "undefined reference" errors. I have made a small code example which reproduces the problem. All 3 source files are in the same directory. I'm pretty sure I'm missing something basic but I'v not been able to figure out what.

Error message:

sketch.ino.cpp.o: In function `loop':
C:\Users\Emil\Dropbox\prog\arduino-1.0.6/sketch.ino.ino:11: undefined reference to `mylib::area(float)'
sketch.ino.cpp.o: In function `setup':
C:\Users\Emil\Dropbox\prog\arduino-1.0.6/sketch.ino.ino:7: undefined reference to `mylib::doSomething()'

Sketch:

#include <Arduino.h>
#include "mylib.h"

mylib test(3.14159);

void setup() { 
  test.doSomething(); 
}

void loop() {
  float a = test.area(1.2); 
}

Header file (mylib.h):

#ifndef mylib_h
#define mylib_h
#include <Arduino.h>

class mylib {
  public:
    mylib(float pi);
    float area(float radius);
    void doSomething();
  private:
    int _pi;
};

#endif

cpp file (mylib.cpp):

#include <Arduino.h>
#include "mylib.h"

int _pi;

mylib::mylib(float pi) {
  _pi = pi;
}

void doSomething() {
  //does nothing
}

float area(float radius) {
  float areaofcircle = _pi*(radius*radius);
  return areaofcircle; 
}

Do all three files show up as tabs in your sketch window?

Try putting a libraries/mylib folder in your sketch folder (My Documents/Arduino?) and put the two mylib files there. Then re-start the IDE to register your library.

If you want to get fancy, put your sketch into libraries/mylib/examples/sketch/sketch.ino Then when you re-start the IDE you should be able to find your sketch in File->Examples->mylib->sketch

In the .cpp file, put mylib:: before the method names. For example

mylib::doSomething()

Hackscribble:
In the .cpp file, put mylib:: before the method names. For example

mylib::doSomething()

It compiles without error now. Thank you.

I don't know why, but I thought classname:: was only for the constructor.

Emil123:
I don't know why, but I thought classname:: was only for the constructor.

It is for anything associated with the class, which isn't defined within the class declaration itself. The special case for the constructor function is that it doesn't have a return type (it is assumed it returns an object of the class type.)

What you did was declare two functions in the class, then define two functions that are not part of the class. The two class functions were never defined, and the two other functions were never declared.

You would also use the classname:: prefix to define storage for any static class members (one variable instance shared by all instances of the class.) Normally, when you declare a class member within the class declaration, the storage for it is allocated when the class instance is created. But if you declared it static (like you might be inclined to do with the _pi member variable, since one would assume pi is the same in all instances) then no storage is implicitly defined. You would have to add an explicit definition of the storage by including this in your mylib.c file:

float mylib::_pi;