Unable to match function definition to an existing declaration



  • Hello!

    I'm getting the following error:

    error C2244: 'B<T>::GetM' : unable to match function definition to an existing declaration
    d:\anton\_projects\software\cpp\study\testmember\main.cpp 20
    

    in the following code:

    template<class T> class A
    {
    public:
    	typedef T *pointer;
    };
    
    template<class T> class B
    {
    public:
    	typedef typename A<T>::pointer pointer;
    
    	pointer m;
    
    	inline pointer GetM(void) const;
    };
    
    template<class T> inline typename B<T>::pointer B<T>::GetM(void) const
    {
    	return m;
    }
    
    int main(int const argc, char const *const *const argv)
    {
    	return 0;
    }
    

    I found that that error disappears if I do one of the following:

    1. Change line 14 to the "inline **typename B<T>::**pointer GetM(void) const;"
    2. Change line 17 to the "template<class T> inline typename A<T>::pointer B<T>::GetM(void) const"

    Why does this happen? What I'm doing wrong?

    P.S. Please answer in English if you can.


  • Mod

    There's nothing wrong with your code. The same can't be said about the compiler that you're using.



  • Which Version of MS VC Compiler do you use?

    Gnu g++ 4.2 compiles your bit of code without any complaints. (including -ansi and -pedantic!)


  • Mod

    Actually, there is something wrong with your code but none that would explain the error you get:

    int main(int const argc, char const *const *const argv)
    

    Although an implementation is allowed to permit that signature for the main function it is not required to do so. The only signatures the standard requires to be usable are:

    int main(); // and
    int main(int argc, char** argv)
    

    adding const as a top-level qualifier is allowed since that doesn't change the signature, but not as a qualifier of the pointee of argv.



  • I am using Microsoft® Visual Studio® 2005 (version 8.0.50727.42)

    How I can copy with my problem?

    P.S.: About arguments of main(). Can I use "int main(int const argc, char **const argv)" as the main function of a program ??



  • Another similar question:

    Why I can compile the following code without errors/warnings (see line 19)?

    template<class T> class A
    {
    public:
    	typedef T *pointer;
    };
    
    template<class T> class B
    {
    public:
    	typedef typename A<T>::pointer pointer;
    
    	pointer m;
    
    	inline typename B<T>::pointer GetM(void) const;
    };
    
    template<class T> inline typename B<T>::pointer B<T>::GetM(void) const
    {
    	Something wrong!
    }
    
    int main(int const argc, char const *const *const argv)
    {
    	return 0;
    }
    


  • ...weil die deklaration der implementierung entspricht.



  • It is quite likely that you can compile the second code as long as the compiler is not forced to instantiate any templates. It may ignore this code then (also I don't know if this is permitted by the ISO Standard



  • OK, I can instantiate it. Still no errors:

    template<class T> class B
    {
    public:
    	T m;
    	inline T GetM(void) const;
    };
    
    template<class T> T B<T>::GetM(void) const
    {
    	Something wrong!
    }
    
    int main(int const argc, char const *const *const argv)
    {
    	B<int> b;
    
    	return 0;
    }
    


  • Can you even compile this:

    template<class T> class B 
    { 
    public: 
        T m; 
        inline T GetM(void) const; 
    }; 
    
    template<class T> T B<T>::GetM(void) const 
    { 
        Something wrong! 
    } 
    
    int main(int const argc, char const *const *const argv) 
    { 
        B<int> b; 
        b.GetM();
    
        return 0; 
    }
    

    REMARK://
    g++ does generate an error for your code:

    main.cpp: In member function 'T B<T>::GetM() const':
    main.cpp:10: error: 'Something' was not declared in this scope
    main.cpp:10: error: expected ';' before 'wrong'
    

    even with an empty main()


  • Mod

    SAn schrieb:

    I am using Microsoft® Visual Studio® 2005 (version 8.0.50727.42)

    How I can copy with my problem?

    Install the Service Pack.



  • I have installed SP1 for Visual Studio 2005.

    The Service Pack is 430 megabytes in size! (Is it intended to fix 430 megabytes of bugs??)
    It takes 12 hours to install it.
    After 4 hours of installation it say that there is no enough free space on the drive C: (it was 3GB free space at the beginning of installation).
    I have to clean up 2GB more for install to finish.

    After installation finished the free space reduced by 1.5Gb. It takes me several hours to find and delete unnecessary files left by the installer (in "...\Local settings\Temp", "C:\Config.Msi" and other folders).

    😞

    But now the problem is solved. Thank you

    🙂


  • Gesperrt

    Hi everyone,

    I'm having a similar issue with matching function definitions to existing declarations in my C++ program. I'm getting an error message that says "no matching function for call" and I'm not sure what the problem is.

    I've tried adjusting the parameters in my function and making sure that the types match, but I'm still getting the same error. I'm wondering if there's something else that I'm missing.

    If anyone has any insights or suggestions, I would greatly appreciate it. I'm fairly new to C++ programming, so any help would be very helpful.

    Thanks in advance!


  • Mod

    Open a new thread with the relevant code and precise questions. Nobody wants to read 2 pages of posts from 2007 that are only tangentially related to your problem.

    Your error message means exactly what it says: You are trying to call a function that (in that form as you called it) does not exist. The most likely reason for that is that you did just that, maybe it is a typo or wrong parameters.


Anmelden zum Antworten