C ++ error: member announcement not found

I am new to C ++. Today I have a problem: in the header file, I define the class:

template<class T> class Ptr_to_const { private: Array_Data<T>* ap; unsigned sub; public: ... Ptr_to_const<T> & operator=(const Ptr_to_const<T> & p); }; 

and in the source file I program as:

 template<class T> Ptr_to_const<T>& Ptr_to_const<T>::operator=( const Ptr_to_const<T> & p) { ... return *this; } 

when compiling, the compiler always says: ' Member declaration not found . why?

I am using eclipse CDT + Cygwin GCC

Thank you very much!

+8
c ++ declaration member
source share
3 answers

Template classes must be declared and defined in the header or other file that is included by users. They cannot be declared in the header and defined in the source file, as usual.

The reason is that the template must be replaced with the actual type and source for the generated and compiled one when using it, and the compiler, of course, cannot precompile the templates for all possible types that may arise, so users should be (and therefore need access to code).

This causes some problems when transferring objects if several libraries contain the same templates, since they can be compiled in different versions of the header (see definition rule).

+6
source share

"Member declaration not found" is an error created by the Eclipse ( codan ) static analysis tool, not the compiler. If you get this error, but compilation succeeds, this is a false positive. Old versions of this tool are known to produce some false positives; see, for example, this error report . Therefore, I recommend updating Eclipse CDT to the latest version. If this does not help, send a bug report to the Eclipse CDT.

However, if you also get errors from the compiler (this indicates a C / C ++ problem in the Type column in the Problems view), you probably forgot to include the header file.

+1
source share

You must include the source file at the end of the header file, or you define a member function in the header file when defining a template class

0
source share

All Articles