Linker error on creating new class files


#1

Hi I created a new project on mac with tinderBox. I made a basic app to draw cube and it worked then I tired to create a particle class by creating cpp file and h file in source directory… it does not have any errors but it fails to compile with this error.

Undefined symbols for architecture x86_64:
"tourApp::p()", referenced from:
tourApp::draw() in tourApp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

this is my apps class declaration

class tourApp : public App {
 public:
void setup() override;
void mouseDown( MouseEvent event ) override;
void update() override;
void draw() override;
void prepareSettings(Settings *settings);
CameraPersp cam;
//add a particle p for making a globaly accessable particle object
particle p();
};

and just calling p().draw() in draw() method. this types of errors kill lot of fun time. :frowning:


#2

p is a variable of type particle right? Then it should be declared with

particle p;

And called with

p.draw();

#3

Thanks that worked. well how about calling constructor of p? I read tutorial and there the would initialize it as Particle p(some data , some data2); … thats i how I made it. and p().draw(); was Xcode’s suggestion I had first written p.draw(); but it was showing error.

Any ways Thanks @koenaad. :slight_smile:


#4

You can use curly brackets like this:

particle p{ 100, 100 };

That’s called list initialization, which is new from C++11.

Or call the constructor like this:

particle p = particle( 100, 100 );

List initialization is usually preferred over constructor + assignment. Although the difference would be trivial in this case.

If you use round brackets the compiler thinks you are defining a function. That’s why xcode suggested using p().draw().