Error 2 Error C2758
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 references must be initialized c++ about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
A Member Of Reference Type Must Be Initialized C++
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each ue4 references must be initialized other. Join them; it only takes a minute: Sign up 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 error c2530 source-code below. Can anybody describe why this error exists and how I can fix it? Error 1 error C2758: 'A::s_' : must be initialized in constructor base/member initializer #include
Provides No Initializer For Reference Member
} private: string& s_; }; int main() { return 0; } c++ constructor compiler-errors visual-studio-2012 share|improve this question asked 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
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 must be initialized by constructor not by this site About Us Learn more about Stack Overflow the company Business initializer list in c++ 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 http://stackoverflow.com/questions/12890438/reference-varable-with-error-must-be-initialized-in-constructor-base-member-ini up Must a reference type be initialized in constructor initialization list? up vote 6 down vote favorite 1 As self-exercise, I have written this simple code: #include
New Topic/Question Reply 3 Replies - 2563 Views - Last Post: 29 May 2009 - 08:19 PM Rate Topic: #1 Squee New D.I.C Head Reputation: 0 Posts: 4 Joined: 24-April 09 Error with initializing reference. Posted 29 May http://www.dreamincode.net/forums/topic/107667-error-with-initializing-reference/ 2009 - 04:26 PM Alright I have a problem in C++ (which I'm very new to). I'm trying to do some stuff with references to input streams and I end up with errors which I don't http://www.gamedev.net/topic/552551-how-do-you-initialize-a-reference-type-in-default-constructor/ understand (and which are almost certainly very simple, but google didn't help). In my header file wordstream.h the relevant lines are these. class WordStream { public: private: std::ifstream& inFile; public: WordStream(std::ifstream& fileStream); }; And the constructor in must be wordstream.cpp is as follows. WordStream::WordStream(std::ifstream& fileStream) { inFile = fileStream; } When I try and compile I get the error "error C2758: 'WordStream::inFile' : must be initialized in constructor base/member initializer list". I'm assuming its a problem with what I've done with references here but to be honest I'm pretty well stuck and can't figure out what it is I've stuffed up. Cheers for any help you guys can give. This post has been must be initialized edited by Squee: 29 May 2009 - 04:31 PM Is This A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: Error with initializing reference. #2 crazyjugglerdrummer GAME OVER. NERD WINS. Reputation: 122 Posts: 690 Joined: 07-January 09 Re: Error with initializing reference. Posted 29 May 2009 - 06:54 PM I had this exact same problem a while ago. What you need is an initializer list for the constructor. This basically takes the arguments passed to the constructor, and uses them to initialize references in the class. Change your constructor: WordStream::WordStream(std::ifstream& fileStream) : infile(fileStream) {} This takes the infile instance variable and initializes it with the contents of the argument fileStream. You see this alot when working with refs. You don't need anything in the constructor body, unless you want to do other stuff. Was This Post Helpful? 1 Back to top MultiQuote Quote + Reply #3 Squee New D.I.C Head Reputation: 0 Posts: 4 Joined: 24-April 09 Re: Error with initializing reference. Posted 29 May 2009 - 07:07 PM Hey thanks for that. Solved my problem entirely! Was This Post Helpful? 0 Back to top MultiQuote Quote + Reply #4 Squee New D.I.C Head Reputation: 0 Posts: 4 Joined: 24-April 09 Re: Error with initializing reference. Posted 29 May 2009 - 08:1
topic ForumsMembersHelp Files Developer Journals Gallery Calendar Downloads Resources Store Classifieds Tracker Links Home For Beginners Articles All Articles Post an Article Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL and Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile Development Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Music and Sound Visual Arts Business Breaking into the Industry Production and Management Interviews Business and Law Event Coverage Forums All Forums Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL & Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile & Console Development Build Systems & Source Control Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Writing For Games Music and Sound Visual Arts Gallery Business Breaking into the Industry Business & Law Production & Management Community GDNet Lounge Coding Horrors Article Writing Comments, Suggestions and Ideas Your Announcements Hobby Project Classifieds Indie Project Showcase Community Developer Journals GDNet+ Subscriptions Gallery Store Classifieds Jobs Freelancers Hobby Projects Marketplace Newsletter » Home » Forums » The Technical Side » General Programming Chat Watched Content New Content 0 How do you initialize a reference type in default constructor? Started by chadsxe, Nov 05 2009 04:17 PM Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. You cannot reply to this topic 17 replies to this topic #1 chadsxe Members -Reputation: 146 Like 0Likes Like Posted 05 November 2009 - 04:17 PM Kinda hard to explain but I am getting this error error C2758: 'Graphics::m_applicationReferenc