Thread: compile error about member variable initialization

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    compile error about member variable initialization

    Hello everyone,


    Why the below code segment will result in compile error? When I change code to the comment one (constructor), it can compile. The compiler is too stupid? :-)

    I am using Visual Studio 2005.

    --------------------
    main.cpp(13) : error C2758: 'Foo::vi' : must be initialized in constructor base/member initializer list

    see declaration of 'Foo::vi'
    --------------------

    Code:
    #include <vector>
    
    using namespace std;
    
    class Foo {
    
    private:
    
    	vector<int>& vi;
    
    public:
    
    	Foo (vector<int>& vi_in)
    	{
    		vi = vi_in;
    	}
    /*
    	Foo (vector<int>& vi_in) : vi (vi_in)
    	{
    	}
    */
    };
    
    int main()
    {
    	vector<int> vi;
    	Foo foo = Foo (vi);
    
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The compiler is right. References must be initialized and assignment (in constructor body) is not initialization.

    Regarding having a reference member: the vector outside the class must have a longer lifetime than Foo, or the member reference becomes invalid.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To do that, you need to use a pointer (or just taking a copy of the object). References aren't perfect -_-
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    To do that, you need to use a pointer (or just taking a copy of the object). References aren't perfect -_-
    I don't think there's any real problem. If you need a "reseatable" reference then use a pointer, but in this case I think it's a simple case of "Just wasn't doing it right."

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, you could probably get away with references here since the OP is passing the array in the constructor. But if you need to assign the vector member at a later stage than the constructor, a pointer is necessary; otherwise the code is perfectly fine.
    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. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  2. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM
  3. Data member initialization
    By Fyodorox in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2002, 11:09 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. structure member initialization
    By ivandn in forum C Programming
    Replies: 4
    Last Post: 10-27-2001, 01:27 PM