Error C2758 Must Be Initialized In Constructor Base/member Initializer List
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with
References Must Be Initialized C++
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack ue4 references must be initialized Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up a member of reference type must be initialized c++ Reference varable with error, must be initialized in constructor base/member initializer up vote 2 down vote favorite 1 I got following error when I try to compile the source-code below. Can anybody describe why this error exists and how I can
Provides No Initializer For Reference Member
fix it? Error 1 error C2758: 'A::s_' : must be initialized in constructor base/member initializer #include
Must Be Initialized By Constructor Not By
Oct 15 '12 at 6:36 Number42 10228 1 You can't really have a semantically valid assignment operator when you have a reference member. You can't reassign it to a different string. It will only modify the original string being referenced. –Peter Wood Oct 15 '12 at 7:05 add a comment| 3 Answers 3 active oldest votes up vote 7 down vote accepted First of all, your A::s_ is a reference to a std::string; that means that it's referencing something that must exists somewhere. Due of his reference type, and the fact that the references must be initialized at the moment they're created, you must initialize A::s_ in ALL the A constructors (as pointed by other users): class A { public: A(string& s) : s_(s) { cout << "A::ctor" << endl; } A(const A& rhs) : s_(rhs.s_) // <-- here too!! { cout << "A::copy" << endl; } ~A() { cout << "A::dtor" << endl; } A& operator=(const A& rhs) { cout << "A::copyassign" << endl; } private: string& s_; }; And now, back to the first thing I mentioned; the A::s_ must reference something that exists, so you must be aware of some things, take a look at the following code: int main() { // New A instance: A a("hello world"); return 0; } Constructing this A instance we're providing a const char[12] value, with this value a temporary std::string is created and is given to the A::A(string& s) constructor. Where A::s_ is referencing after the constructor ends? W
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of initializer list in c++ this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/12890438/reference-varable-with-error-must-be-initialized-in-constructor-base-member-ini const reference must be initialized in constructor base/member initializer list up vote 7 down vote favorite 2 I am trying to block access to the default constructor of a class I am writing. The constructor I want others to use requires a const reference to another object. I have made the default constructor private to prevent others from using it. I am getting http://stackoverflow.com/questions/3168238/const-reference-must-be-initialized-in-constructor-base-member-initializer-list a compiler error for the default constructor because the const reference member variable is not initialized properly. What can I do to make this compile? class CFoo { public: CFoo(); ~CFoo(); }; class CBar { public: CBar(const CFoo& foo) : fooReference(foo) { } ~CBar(); private: const CFoo& fooReference; CBar() // I am getting a compiler error because I don't know what to do with fooReference here... { } }; c++ constructor reference initialization share|improve this question asked Jul 2 '10 at 18:30 Trevor Balcom 1,56311125 add a comment| 2 Answers 2 active oldest votes up vote 12 down vote accepted don`t declare default constructor. It is not available anyway (automatically that it's) if you declare your own constructor. class CBar { public: CBar(const CFoo& foo) : fooReference(foo) { } private: const CFoo& fooReference; }; fairly comprehensive explanation of constructors can be found here: http://www.parashift.com/c++-faq-lite/ctors.html share|improve this answer edited Jul 2 '10 at 18:54 answered Jul 2 '10 at 18:34 Anycorn 24.8k23109208 1 Thank you for the help. For some reason, I thought the compiler generated a default constructor even if I did not write one. &ndas
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards https://msdn.microsoft.com/en-us/library/aa748768(v=vs.60).aspx Events Community Magazine Forums Blogs Channel 9 Documentation APIs and http://forums.codeguru.com/showthread.php?345506-Initialization-of-constructor-base-member reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Visual C++ Programmer's Guide Build Errors Compiler Errors C2700 Through C2788 Compiler Errors C2700 Through C2788 Compiler Error must be C2758 Compiler Error C2758 Compiler Error C2758 Compiler Error C2700 Compiler Error C2702 Compiler Error C2703 Compiler Error C2704 Compiler Error C2705 Compiler Error C2707 Compiler Error C2708 Compiler Error C2709 Compiler Error C2710 Compiler Error C2711 Compiler Error C2712 Compiler Error C2713 Compiler Error C2720 Compiler Error C2721 Compiler Error must be initialized C2722 Compiler Error C2723 Compiler Error C2724 Compiler Error C2730 Compiler Error C2731 Compiler Error C2732 Compiler Error C2733 Compiler Error C2734 Compiler Error C2735 Compiler Error C2736 Compiler Error C2737 Compiler Error C2750 Compiler Error C2758 Compiler Error C2760 Compiler Error C2770 Compiler Error C2771 Compiler Error C2772 Compiler Error C2773 Compiler Error C2774 Compiler Error C2775 Compiler Error C2776 Compiler Error C2777 Compiler Error C2778 Compiler Error C2779 Compiler Error C2780 Compiler Error C2781 Compiler Error C2782 Compiler Error C2783 Compiler Error C2784 Compiler Error C2785 Compiler Error C2786 Compiler Error C2787 Compiler Error C2788 TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Compiler Error C2758 Visual Studio 6.0 'const' : must be initialized in constructor base/member initializer list The given constant was not initialized with a
Forum Visual C++ & C++ Programming C++ (Non Visual C++ Issues) Initialization of constructor base/member If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Results 1 to 14 of 14 Thread: Initialization of constructor base/member Tweet Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode June 17th, 2005,11:29 AM #1 KaramChand03 View Profile View Forum Posts Member Join Date Nov 2003 Posts 211 Initialization of constructor base/member Hello, I have a typedefed variable: typedef Obj * &POBJ; In one of my classes say A: Code: class A { public: A(); ~A(); POBJ a; }; If I declare it in my cpp: Code: A a; I get an error from my compiler: 'variablename' : must be initialized in constructor base/member initializer list I try to change my constructor like: Code: A::A() : a ( NULL ) { } It throws up an error saying to specify some default value. I know i am doing some simple mistake with my implementation of C++ but I read Ivor Horton and couldnt find a solution. Maybe I am too naive. Its the same with structures too: Code: struct struc { POBJ a; }; And in the CPP file: Code: struc * a = new struc; Throws up the same variable initializer error. How can I solve it? I require POBJ as a member variable. I am using VC 6.0 if that helps. Reply With Quote June 17th, 2005,11:39 AM #2 Siddhartha View Profile View Forum Posts Visit Homepage Elite Member Power Poster Join Date Oct 2002 Location Germany Posts 6,205 Re: Initialization of constructor base/member Originally Posted by KaramChand03 I have a typedefed variable: typedef Obj * &POBJ; What "Type" is Obj? So, asssume a class CMyClass, you can type-define a pointer to it as: Code: typedef CMyClass* PMYCLASS; You would then use it in class A, like this: Code: class A { public: A(); ~A(); PMYCLASS m_pMyClassPtr; //