Expected compile-time constant expression

This is a discussion on Expected compile-time constant expression within the C++ Programming forums, part of the General Programming Boards category; Does anyone know why the following works: Code: class Test { public: void Set(int) {} int Get() {} Test(): m_Test(this) ...

  1. #1
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423

    Expected compile-time constant expression

    Does anyone know why the following works:
    Code:
    class Test
    {
    public:
    	void Set(int) {}
    	int Get() {}
    
    	Test(): m_Test(this) {};
    	Stuff::CUtilityMember<int, Test, int, &Test::Get, &Test::Set> m_Test;
    };
    But not the following:
    Code:
    class Test
    {
    public:
    	void Set(int) {}
    	int Get() {}
    
    	Test(): m_Test(this) {};
    	Stuff::CUtilityMember<int, Test, int, NULL, NULL> m_Test;
    };
    ?

    CUtilityMember looks like:
    Code:
    #define Template	template \
    					< \
    						typename Type, typename Class, typename ReturnType, \
    						/*typename*/ /*ParamPolicy<ReturnType>::Type*/int (Class::* GetFunction)(), \
    						void (Class::* SetFunction)(/*typename ParamPolicy<Type>::Type*/int NewData) \
    					>
    Error 3 error C2975: 'GetFunction' : invalid template argument for 'Stuff::CUtilityMember', expected compile-time constant expression g:\w00t\visual studio 2008\projects\stuff\utilitymember.h 65
    Error 4 error C2975: 'SetFunction' : invalid template argument for 'Stuff::CUtilityMember', expected compile-time constant expression g:\w00t\visual studio 2008\projects\stuff\utilitymember.h 65
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I've a feeling it's with 'int NewData'... try replacing that with a compile-time constant expression, like 42, for example and see if that compiles. Just guessing. I can't replicate the error with what you've provided here.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Oh! I didn't even SEE that error parameter name there. I just moved those from the class, so of course I made a mistake. Meh.
    But it still doesn't solve the problem by removing it.
    I can attach the source for anyone to try to compile and see if they can figure out why it's not accepting 0 as a compile-time expression.
    Attached Files Attached Files
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'm not sure why, but if I put the body of the constructor within the class definition it compiles...
    Code:
    	class CUtilityMember
    	{
    	private:
    		Type Data;
    		//typedef const ReturnType& (Class::*GetFncPointer)();
    		//typedef void (Class::*SetFncPointer)(const Type& NewData);
    		//GetFncPointer m_pGet;
    		//SetFncPointer m_pSet;
    		Class* m_pClass;
    	
    	public:
        CUtilityMember(Class* pClass) //, GetFncPointer pGet = NULL, SetFncPointer pSet = NULL);
        : m_pClass(pClass){  }
    
    		CUtilityMember(typename ParamPolicy<ReturnType>::Type NewData, Class* pClass); //, GetFncPointer pGet = NULL, SetFncPointer pSet = NULL);
    		CUtilityMember& operator = (typename ParamPolicy<ReturnType>::Type NewData);
    		operator Type () const;
    	};
    .

    Edit: Also doing this works too:
    Code:
    #define TemplateArgs\
    						typename Type, typename Class, typename ReturnType, \
    						/*typename*/ /*ParamPolicy<ReturnType>::Type*/int (Class::* GetFunction)(), \
    						void (Class::* SetFunction)(/*typename ParamPolicy<Type>::Type*/int) 
    #define Template	template<TemplateArgs> 
    
    // ...
    
    	Template CUtilityMember<TemplateArgs>:://...
    	Template CUtilityMember<TemplateArgs>:://...
    etc
    Last edited by twomers; 09-26-2008 at 10:19 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Weird. You're right that it works if the constructor is placed in the class body...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM
  5. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21