Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: member function may not be declared outside of its class.

Tags:

c++

I have my heap.h file that contains:

bool insert(int key, double data);

and in my heapCPP.cpp file I have:

 bool heap::insert(int key, double data){
    bool returnTemp;
    node *temp = new node(key, data);

    returnTemp = insert(temp);
    delete temp;
    return returnTemp;
}

However, I get a error saying "member function "heap::insert" may not be redeclared outside its class.

like image 740
grillo Avatar asked Apr 23 '15 18:04

grillo


3 Answers

You possibly forgot a closing bracket in your cpp and this can be interpreted as redeclaring it inside another function.

like image 164
BlueTrin Avatar answered Oct 31 '22 08:10

BlueTrin


The error message is clear enough. If function insert is a member function of class heap it shall be at first declared in the class definition.

For example

class heap
{
    //...
    bool insert(int key, double data);
    //,,,
};

Take into account that you are using one more function with name insert inside the body of the first function

returnTemp = insert(temp);

So it seems you have some mess with function declarations and definitions.

like image 42
Vlad from Moscow Avatar answered Oct 31 '22 09:10

Vlad from Moscow


I had the same error message, but in my case the problem was caused by a semi-colon in the .cpp file (from a bad copy and paste). That is, there was a semi-colon at the end of the function signature in the cpp file.

If I use your code as an example then,

heap.h:

bool insert(int key, double data);

heapCPP.cpp:

bool heap::insert(int key, double data);
{
    // ...
}

Fix it with this in heapCPP.cpp:

bool heap::insert(int key, double data)
{
    // ...
}
like image 29
cham Avatar answered Oct 31 '22 08:10

cham