locked
C++: template Class: how initialize it from a derived class? RRS feed

  • Question

  • how can i initialize a template base class from a derived class?

    template <typename CBase>
    class test
    {
    	CBase* atPointer = nullptr;
    public:
    	test(CBase *clsPointer) : atPointer(clsPointer)
    	{
    		if (IS_EVENT_DEFINED(&CBase::MouseClick))
    			atPointer->MouseClick();
    
    
    		if (IS_EVENT_DEFINED(&CBase::Move))
    			atPointer->Move();
    
    	}
    	void call()
    	{
    		if (IS_EVENT_DEFINED(&CBase::MouseClick))
    			atPointer->MouseClick();
    
    		if (IS_EVENT_DEFINED(&CBase::Move))
    			atPointer->Move();
    
    	}
    };
    
    
    class InstanceName : public test<InstanceName> //error
    {
    public:
    	InstanceName() : test(this) { ; }
    	~InstanceName() { ; }
    public:
    //..............
    error message:
    "variable 'InstanceName' isn't a type name"(something like these)

    why these error?

    Monday, May 7, 2018 9:36 PM

Answers

  • Igor: After update the Visual Studio 2017, the template pointer is working normaly.
    thank you so much for all

    Baron Bi: i did now: 
    https://developercommunity.visualstudio.com/content/problem/255607/if-i-create-a-derived-class-with-same-nameon-the-e.html
    thanks for all
    • Marked as answer by Cambalinho Thursday, May 17, 2018 5:16 PM
    Thursday, May 17, 2018 5:15 PM

All replies

  • On 5/7/2018 5:36 PM, Cambalinho wrote:

    how can i initialize a template base class from a derived class?

    The initialization part compiles as written: http://rextester.com/MZPGPS73259 . To the extent there is a problem, it must lie elsewhere.

    [code]
    template <typename CBase>
    class test
    {
      CBase* atPointer = nullptr;
    public:
      test(CBase *clsPointer) : atPointer(clsPointer)
      {
        if (IS_EVENT_DEFINED(&CBase::MouseClick))
          atPointer->MouseClick();

    You are trying to call methods of a derived class from the constructor of its base class. This is dangerous. At this point, the derived class's constructor hasn't completed yet. If those methods touch any data members or any other base classes of CBase, your program exhibits undefined behavior.

    error message:
    "variable 'InstanceName' isn't a type name"(something like these)

    Show the exact and complete text of the error message. Don't paraphrase; specifics matter.

    Monday, May 7, 2018 9:54 PM
  • On 5/7/2018 5:36 PM, Cambalinho wrote:

    how can i initialize a template base class from a derived class?

    The initialization part compiles as written: http://rextester.com/MZPGPS73259 . To the extent there is a problem, it must lie elsewhere.

    [code]
    template <typename CBase>
    class test
    {
      CBase* atPointer = nullptr;
    public:
      test(CBase *clsPointer) : atPointer(clsPointer)
      {
        if (IS_EVENT_DEFINED(&CBase::MouseClick))
          atPointer->MouseClick();

    You are trying to call methods of a derived class from the constructor of its base class. This is dangerous. At this point, the derived class's constructor hasn't completed yet. If those methods touch any data members or any other base classes of CBase, your program exhibits undefined behavior.

    error message:
    "variable 'InstanceName' isn't a type name"(something like these)

    Show the exact and complete text of the error message. Don't paraphrase; specifics matter.

    the error messages are Portuguese.. my Visual Studio it's in Portuguese.

    but you have right... so why i get that error.. did i miss something?

    Monday, May 7, 2018 10:00 PM
  • On 5/7/2018 6:00 PM, Cambalinho wrote:

    On 5/7/2018 5:36 PM, Cambalinho wrote:

    how can i initialize a template base class from a derived class?

    The initialization part compiles as written: http://rextester.com/MZPGPS73259 . To the extent there is a problem, it must lie elsewhere.

    [code]
    template <typename CBase>
    class test
    {
       CBase* atPointer = nullptr;
    public:
       test(CBase *clsPointer) : atPointer(clsPointer)
       {
         if (IS_EVENT_DEFINED(&CBase::MouseClick))
           atPointer->MouseClick();

    You are trying to call methods of a derived class from the constructor of its base class. This is dangerous. At this point, the derived class's constructor hasn't completed yet. If those methods touch any data members or any other base classes of CBase, your program exhibits undefined behavior.

    error message:
    "variable 'InstanceName' isn't a type name"(something like these)

    Show the exact and complete text of the error message. Don't paraphrase; specifics matter.

    the error messages are Portuguese.. my Visual Studio it's in Portuguese.

    but you have right... so why i get that error.. did i miss something?

    Probably. I can't say more without seeing a) the exact text of the error (even if in Portuguese) and b) a small complete example that actually produces this error.

    Monday, May 7, 2018 10:41 PM
  • #include <iostream>
    
    
    template <typename CBase>
    class test
    {
    	CBase* atPointer = nullptr;
    public:
    	test(CBase *clsPointer) : atPointer(clsPointer)
    	{
    		
    			atPointer->MouseClick();
    
    
    		
    			atPointer->Move();
    
    	}
    	void call()
    	{
    		
    			atPointer->MouseClick();
    
    		
    			atPointer->Move();
    
    	}
    };
    
    
    class InstanceName : public test<InstanceName>
    {
    public:
    	InstanceName() : test(this) { ; }
    	~InstanceName() { ; }
    public:
    	void MouseClick();
    	void Move();
    } InstanceName;
    
    void InstanceName::MouseClick()
    {
    std::cout << "Mouse click\n";
    }
    
    
    void InstanceName::Move()
    {
    	std::cout << "Move\n";
    }
    
    
    int main()
    {
    	InstanceName.call();
    	std::cin.get();
    	return 0;
    }

    error on line 31:

    E0757 variável "InstanceName" não é o nome de um tipo

    Monday, May 7, 2018 10:45 PM
  • On 5/7/2018 6:45 PM, Cambalinho wrote:

      InstanceName.call();

    This makes no sense. You call a method on an object, not on a type.

    Monday, May 7, 2018 11:17 PM
  • Igor I am sure knows more than I do but what I am getting seems to indicate there is an error somewhere in the compiler. I get the following from that code.

    That image is not real easy to read but see below.

    I can compile that and I get no error. It executes as expected. Then I added a line for diagnostic purposes and I forgot the semicolon. Then when I compiled I got the error about the missing semicolon and it produces some other strange message; something about a class template instantiation being compiled. It is a "note" with no error message.

    Oh, here we go. When I look in the error list tab I get:

    E0757	variable "InstanceName" is not a type name

    But in the output window I get the "note" without that error. The inconsistency is that I get none of that if I just put a semicolon where it belongs; when I do that the program compiles and executes.



    Sam Hobbs
    SimpleSamples.Info


    Monday, May 7, 2018 11:20 PM
  • On 5/7/2018 6:45 PM, Cambalinho wrote:

    [code]
    class InstanceName : public test<InstanceName>
    {
    } InstanceName;

    Ah. I didn't notice that you define a variable with the same name as the type. You probably don't want to do that, if only to avoid this kind of confusion.

    Anyway, your example compiles exactly as written: http://rextester.com/AZVN43977  . It does not reproduce the error.

    Monday, May 7, 2018 11:28 PM
  • so the best is update my VS 2017.

    yes, when i changed the name, the error isn't showed. but i need with same name.. so how can i avoid these error?

    • Edited by Cambalinho Tuesday, May 8, 2018 10:14 PM
    Tuesday, May 8, 2018 10:02 PM
  • now i have another problem with code:

    template <typename CBase> class test { CBase* atPointer = nullptr; public: test(CBase *clsPointer) : atPointer(clsPointer) {

    if (clsPointer==nullptr)
    std::cout << "error";  //no error

    //if (IS_EVENT_DEFINED(&CBase::MouseClick)) atPointer->MouseClick(); //if (IS_EVENT_DEFINED(&CBase::Move)) atPointer->Move(); } void call() { //if (IS_EVENT_DEFINED(&CBase::MouseClick)) atPointer->MouseClick(); //if (IS_EVENT_DEFINED(&CBase::Move)) atPointer->Move(); } }; class InstanceName : public test<InstanceName> { public: InstanceName() : test(this) { ; } ~InstanceName() { ; } public: void MouseClick(); void Move(); };

    void InstanceName::MouseClick()
    {
        std::cout << "Mouse click\n";
    }


    void InstanceName::Move()
    {
       std::cout << "Move\n";
    }


    why i don't receive the pointer on test constructor?
    why i'm asking?

    because the Move and MouseClick functions aren't called... i belive that i'm not receiving  the pointer correctly

    Thursday, May 10, 2018 8:00 PM
  • On 5/10/2018 4:00 PM, Cambalinho wrote:

    why i don't receive the pointer on test constructor?
    why i'm asking?

    because the Move and MouseClick functions aren't called... i belive that i'm not receiving  the pointer correctly

    Once again, works for me: http://rextester.com/AZVN43977 (same example as last time). Note how it prints "Mouse click" and "Move" twice - once from the constructor, and again from call().

    Thursday, May 10, 2018 8:42 PM
  • On 5/10/2018 4:00 PM, Cambalinho wrote:

    why i don't receive the pointer on test constructor?
    why i'm asking?

    because the Move and MouseClick functions aren't called... i belive that i'm not receiving  the pointer correctly

    Once again, works for me: http://rextester.com/AZVN43977 (same example as last time). Note how it prints "Mouse click" and "Move" twice - once from the constructor, and again from call().

    i'm sorry, so it's best desinstall the VS2017 and then reinstall it?
    Thursday, May 10, 2018 8:52 PM
  • Hi,

    thanks for posting here.

    >>error on line 31:

    E0757 variável "InstanceName" não é o nome de um tipo

    Actually it's a intellisense error, your code compiles fine with my vs2017.

    I suggest you post this issue on this forum below.

    https://developercommunity.visualstudio.com/

    Your understanding and cooperation will be grateful.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 17, 2018 7:32 AM
  • Igor: After update the Visual Studio 2017, the template pointer is working normaly.
    thank you so much for all

    Baron Bi: i did now: 
    https://developercommunity.visualstudio.com/content/problem/255607/if-i-create-a-derived-class-with-same-nameon-the-e.html
    thanks for all
    • Marked as answer by Cambalinho Thursday, May 17, 2018 5:16 PM
    Thursday, May 17, 2018 5:15 PM