CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    Why am I getting a compiler error?

    Here is the code,
    Code:
    class B1
    {
    };
    
    class B2
    {
    public:
    	B2() : m_var(string("")) {}
    	string& m_var;
    };
    
    class D : public B1, public B2
    {
    public:
    	D(string str);
    };
    
    D::D(string str) : B1() , m_var(str)
    {
    }
    
    int main()
    {
    	D d("abc");
    	cout<<d.m_var<<endl;
    
    	return 0;
    }
    I am getting a compiler error "error C2614: 'D' : illegal member initialization: 'm_var' is not a base or member". Why? Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why am I getting a compiler error?

    Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
    Code:
    class B1
    { };
    
    class B2
    {
        public:
    	B2() : m_var(0) {}
    	int m_var;
    };
    
    class D : public B1, public B2
    {
        public:
            D(int s);
    };
    
    D::D(int s) : B1(), m_var(s)
    { }
    
    int main()
    {
        D d(0);
    }
    Here is the error reported by the Comeau compiler:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !

    Your Comeau C/C++ test results are as follows:


    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 17: error: "m_var" is not a nonstatic data member or base
    class of class "D"
    D::D(int s) : B1(), m_var(s)
    ^

    1 error detected in the compilation of "ComeauTest.c".

    In strict mode, with -tused, Compile failed
    The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 29th, 2012 at 12:09 PM.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Why am I getting a compiler error?

    Quote Originally Posted by Paul McKenzie View Post
    ... The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.
    Alternatively, you can provide a constructor for B2 that takes a value to be set to m_var, and initialize B2 instead of that variable.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Why am I getting a compiler error?

    Quote Originally Posted by Paul McKenzie View Post
    Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
    Code:
    class B1
    { };
    
    class B2
    {
        public:
    	B2() : m_var(0) {}
    	int m_var;
    };
    
    class D : public B1, public B2
    {
        public:
            D(int s);
    };
    
    D::D(int s) : B1(), m_var(s)
    { }
    
    int main()
    {
        D d(0);
    }
    Here is the error reported by the Comeau compiler:
    The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.

    Regards,

    Paul McKenzie
    If I change the definition of D constructor like the following, I still get the same error. I thought that before the D constructor is called, B2 has been full constructed. Why'd I still get the same error? Thanks.
    Code:
    D::D(string str) : B2(), m_var(str)
    {
    }

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Why am I getting a compiler error?

    Quote Originally Posted by Paul McKenzie View Post
    Your code gets many more errors due to lack of proper #include files. Let's use an example that actually cuts through all of this noise:
    Code:
    class B1
    { };
    
    class B2
    {
        public:
    	B2() : m_var(0) {}
    	int m_var;
    };
    
    class D : public B1, public B2
    {
        public:
            D(int s);
    };
    
    D::D(int s) : B1(), m_var(s)
    { }
    
    int main()
    {
        D d(0);
    }
    Here is the error reported by the Comeau compiler:
    The issue is that m_var is a non-static member of B2, meaning that the B2 object has to be fully constructed before you can refer to m_var. The only way to set m_var is within the body of the constructor of D, where both B1 and B2 are constructed.

    Regards,

    Paul McKenzie
    If I change the definition of D constructor like the following, I still get the same error. I thought that before the D constructor is called, B2 has been full constructed. Why'd I still get the same error? Thanks.
    Code:
    D::D(string str) : B2(), m_var(str)
    {
    }

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Why am I getting a compiler error?

    Quote Originally Posted by VladimirF View Post
    Alternatively, you can provide a constructor for B2 that takes a value to be set to m_var, and initialize B2 instead of that variable.
    Thanks for your reply. You meant like this?
    Code:
    class B1
    {
    };
    
    class B2
    {
    public:
    	B2(string sVar) : m_var(sVar) {}
    	string& m_var;
    };
    
    class D : public B1, public B2
    {
    public:
    	D(string str);
    };
    
    D::D(string str) : B1() , B2(str)
    {
    }
    
    int main()
    {
    	D d("abc");
    	cout<<d.m_var<<endl;
    
    	return 0;
    }
    But this is not what I want. Basically I thought m_var is member data of B2 and D is derived from B2. So D should contains m_var too. However as Paul pointed out, when I refer to m_var, B2 is not fully constructed, which means m_var doesn't exist yet.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why am I getting a compiler error?

    Quote Originally Posted by LarryChen View Post
    But this is not what I want. Basically I thought m_var is member data of B2 and D is derived from B2. So D should contains m_var too.
    But D does contain m_var. What makes it not so?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Why am I getting a compiler error?

    Quote Originally Posted by Paul McKenzie View Post
    But D does contain m_var. What makes it not so?

    Regards,

    Paul McKenzie
    I agree with you that D does contain m_var. But since B2 object is not fully constructed when m_var is referred, there is a compiler error. So I wonder how to fix that error. Thanks.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why am I getting a compiler error?

    Quote Originally Posted by LarryChen View Post
    I agree with you that D does contain m_var. But since B2 object is not fully constructed when m_var is referred, there is a compiler error. So I wonder how to fix that error. Thanks.
    Why is it important in your design to access m_var at the point where you're trying to access it? Programming isn't just about trying to get something to compile -- what is the overall reason to access non-constructed member variables?

    OK, so m_var isn't available. It is available within the body of the constructor of D or you construct a B() object using the string, and in the constructor of B set m_var to that value. What is the advantage of trying to use m_var as you're using it, even though it is illegal to do so? Why can't the two cases covered by myself and Vladimir not work for your design?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Why am I getting a compiler error?

    Trouble is that in the original code m_var is a reference to string.
    I got it to compile without errors with g++ 4.6.3
    but somehow that feels wrong

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class B1
    {
    };
    
    class B2
    {
    public:
    	B2( string sVar = "" ) : m_var(sVar) {}
    	string & m_var;
    };
    
    class D : public B1, public B2
    {
    public:
    	D(string str);
    };
    
    D::D( string str ) : B1(), B2(str) 
    {
    }
    
    int main()
    {
    	D d("abc");
    	
            cout<<d.m_var<<endl;
    
    	return 0;
    }
    When I run the program it prints in an infinite loop
    Kurt

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why am I getting a compiler error?

    Quote Originally Posted by ZuK View Post
    Trouble is that in the original code m_var is a reference to string.
    I got it to compile without errors with g++ 4.6.3
    but somehow that feels wrong
    It compiles with Comeau C++ also. The issue I have with it is that the string is passed by value, making the parameter a temporary object. I don't know off-hand what will happen to the reference after the function returns and the temporary goes away.

    The wrinkle in this example is that the original string "abc" is a string constant, and not a true std::string. It gets converted to a std::string on the way through, but that converted item disappears has gone when the cout is invoked.


    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 31st, 2012 at 03:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured