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

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.

1

6 Answers

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

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)
{ // ...
}

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.

2

I had the same issue. Check your function definitions before and after your "insert" function definition. Make sure to include closing brackets for your previous function definitions.

My function definition was nested inside another definition which caused my error.

For me, this same error appeared after I removed the topmost initializer from a constructors initializer list after it had become obsolete. My code formatting for constructor definitions looks like this for better readability:

c_MyClass::c_MyClass() : m_bSomeMember{ true } , m_bAnotherMember{ false }
{}

The class I worked with had a lot more initializers, so when I removed the top one as it was no longer needed, I didn't realize that I accidentally removed the colon at the start of the line as well and was left with:

c_MyClass::c_MyClass() , m_bAnotherMember{ false }
{}

Which isn't a constructor definition at all and caused the error.

I had the same error, then I found out that I forgot the semi column while defining the member function right after the function's declaration in my class:

class Fixed{ private: int number; static const int fraction = 8; public: Fixed(const int integer); Fixed(const Fixed& obj); Fixed(const float number); Fixed(); ~Fixed(); Fixed& operator=(const Fixed& pos); int toInt(void) const; float toFloat(void) const; int getRawBits(void) const; void setRawBits(int const raw); //extra overloading bool operator>(const Fixed& pos) const; bool operator<(const Fixed& pos) const; bool operator>=(const Fixed& pos) const; bool operator<=(const Fixed& pos) const; bool operator==(const Fixed& pos) const; bool operator!=(const Fixed& pos) const; Fixed operator+(const Fixed& pos) const; Fixed operator-(const Fixed& pos) const; Fixed operator*(const Fixed& pos) const; Fixed operator/(const Fixed& pos) const; Fixed& operator++(void); Fixed operator++(int); Fixed operator--(int); Fixed& operator--(void);
};

The mistake I made:

Fixed& Fixed::operator--(void);
{ this->number--; return (*this);
}

It can be fixed by removing the semi column:

Fixed& Fixed::operator--(void)
{ this->number--; return (*this);
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like