class - The popular (c++) "error: no matching function for call to ... ", with a kick though -


i working in c++.

my class animation.h following:

class animation { public:     animation(ofvec2f _startloc, ofvec2f _endloc, float _speed, float _accel);     virtual ~animation(); }; 

then in scenemusic.h have class calls animation class.

#include "animation.h" class scenemusic {     public:         scenemusic();         virtual ~scenemusic();     private:         animation stavemotion; }; 

and scenemusic.cpp

scenemusic::scenemusic() {     staveimg.loadimage("./scenemusic/stave_white.png");      stavemotion = animation(ofvec2f(29, -staveimg.height()), \                             ofvec2f(29, 249), 1, 0.1); } 

the above gives following (expected) error:

src/awards/scenemusic.cpp|3|error: no matching function call ‘animation::animation()’| src/awards/scenemusic.cpp|5|note: candidates are:| src/animation.h|14|note: animation::animation(ofvec2f, ofvec2f, float, float)| src/animation.h|14|note:   candidate expects 4 arguments, 0 provided| 

i read alot of threads suggest should use initializer lists , in scenemusic.cpp

scenemusic::scenemusic()     :animation(ofvec2f(29, -staveimg.height()), \                ofvec2f(29, 249), 1, 0.1) {     staveimg.loadimage("./scenemusic/stave_white.png"); } 

or similar. problem height of staveimg become available once constructor executed.

also class scenemusic not inherit class animation, therefore not sure how above work. if attempt run above

src/awards/scenemusic.cpp|4|error: type ‘animation’ not direct base of ‘scenemusic’| src/awards/scenemusic.cpp|4|error: ‘((scenemusic*)this)->scenemusic::staveimg.ofimage_<unsigned char>::height’ cannot used function| src/awards/scenemusic.cpp|5|error: no matching function call ‘animation::animation()’| src/awards/scenemusic.cpp|5|note: candidates are:| ... 

what doing wrong ?

thank help

well, issue second attempt you'd need this:

scenemusic::scenemusic()     :stavemotion(ofvec2f(29, -staveimg.height()), \                ofvec2f(29, 249), 1, 0.1) 

you specify member names in initializer lists. specify class name when attempting call specific super-class constructor. animation not super-class of scenemusic.

the following "no matching function call animation::animation()" occurred same reason have occurred if didn't have in initializer list @ all: stavemotion member still trying use animations default constructor (just default constructor used when declare variable animation a;), animation not have default constructor.

correcting error in initializer list takes care of both errors.

by way, second error regarding height(): make sure there's member function named height(). aren't showing code error seems indicate have member variable named height, not function.

however, above still won't fix issue having call loadimage first (thanks chris reminding me in comments).

the easiest solution revert first attempt, , provide default (no parameter) constructor animation. declare non-default constructor, compiler no longer generates default constructor you, , have explicitly declare/define animation::animation() provide it. way, stavemotion can constructed default values (and won't have constructed in initializer list) , assigned proper value later.

a second option, way, wouldn't require creation of temporary default animations first (or perhaps more importantly, wouldn't require implement default constructor possibly breaks class invariants), have staveimg member field of scenemusic, give constructor takes image filename , loads image, , declare before stavemotion in member list. wouldn't need animation default constructor this, , initialize in initializer list:

scenemusic :: scenemusic () :    staveimg("./scenemusic/stave_white.png"),    stavemotion(ofvec2f(29, -staveimg.height()), ofvec2f(29, 249), 1, 0.1) { } 

a variation on option construct image ahead of time , pass parameter scenemusic constructor. wouldn't require new constructor fo staveimg.

a third option, cantchooseusernames brought in comments, make stavemotion pointer. can initialize null , construct new animation(...) when necessary without requiring default constructor. if this, don't forget delete when done. automatic pointer such std::unique_ptr<animation> or 1 of other smart pointers (boost has too) can make easier on you.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -