Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out-of-line definition error on a class but it is declared in the header file [closed]

Now this is a weird problem. I was coding two days ago and stopped and then continued just now. On my header file (Fruit.h) I added a method called animateGrow() like so:

Fruit.h:

class Fruit {
private:
   // Member variables here

public:
   // Other methods here
   void animateGrow( );
};

But when I try to add the same method in the CPP file, I get an Out-of-line definition of 'animateGrow' does not match any declaration in 'Fruit' error. It's declared in the header but Xcode does not seem to be able to find that method.

Fruit.cpp:

#include "SimpleAudioEngine.h"
#include "Fruit.h"
#include "Tree.h"

using namespace cocos2d;
using namespace CocosDenshion;

Fruit::Fruit( ) {
   // Constructor
}

// Getter Methods
// Setter Methods
// Other Methods

void Fruit::animateGrow( ) {
   // I get an error here when I type it.
}

Full Code: (links removed) (In the code, the Tree class exists and all other methods and functions are working fine except for the animateGrow() as it gives me the error)

like image 905
alxcyl Avatar asked Aug 09 '12 03:08

alxcyl


People also ask

How to separate header and implementation files C++?

You leave the declarations in the header file: class A2DD { private: int gx; int gy; public: A2DD(int x,int y); // leave the declarations here int getSum(); }; And put the definitions in the implementation file. You could mix the two (leave getSum() definition in the header for instance).

How to define class in Cpp file?

Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a . cpp file of the same name as the class. Now any other header or code file that wants to use the Date class can simply #include "Date. h" .

What to include in header file C++?

To minimize the potential for errors, C++ has adopted the convention of using header files to contain declarations. You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration.

What is the. h file in C++?

A file saved with h file extension is a header file used in C/C++ files to include the declaration of variables, constants, and functions. These are referred by the C++ implementation files that contain the actual implementation of these functions.


1 Answers

Fixed it.

I don't know why but Xcode did not save my changes on the header file. I closed Xcode and opened the header file and the changes aren't there. I added the methods again and saved. I opened the CPP file added the new method in it worked fine.

Really weird.

like image 122
alxcyl Avatar answered Sep 19 '22 18:09

alxcyl