Error Class Does Not Have Any Field Named
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 does not have any field named protected Learn more about Stack Overflow the company Business Learn more about hiring developers or
C++ Constructor Inheritance
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow c++ class inheritance 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 C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field? up vote 22 down vote favorite 2 I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access. //The base class: class BaseClass { public: BaseClass(std::string); BaseClass(const BaseClass& orig); virtual ~BaseClass(); const std::string GetData() const; void SetData(const std::string& data); protected: BaseClass(); std::string m_data; }; BaseClass::BaseClass(const std::string data) : m_data(data) { } BaseClass::BaseClass() { } BaseClass::BaseClass(const BaseClass& orig) { } BaseClass::~BaseClass() { } void BaseClass::SetData(const std::string& data) { m_data = data; } const std::string BaseClass::GetData() const { return m_data; } //The derived class: class DerivedClass : public BaseClass { public: DerivedClass(std::string data); DerivedClass(const DerivedClass& orig); virtual ~DerivedClass(); private: }; DerivedClass::DerivedClass(std::string data) : m_data(data) { } //ERROR HERE DerivedClass::DerivedClass(const DerivedClass& orig) { } DerivedClass::~DerivedClass() { } //The compiler error DerivedClass.cpp:3: error: class ‘DerivedClass’ does not have any field named ‘m_data’ Any help is greatly appreciated. Thank you in advance. c++ inheritance constructor protected access-specifier share|improve this question edited Jul 23 '10 at 15:56 JaredPar 451k859221227 asked Jul 23 '10 at 15:51 user343424 140116 1 Thanks guys. That was really helpful! –Ruairi Hourihane Mar 27 '11 at 17:07 add a comment| 6 Answers 6 active oldest votes up vote 33 down vote accepted You cannot initialize m_data in the derived class constructor but instead pass it as an argument to the base class constructor. That is: DerivedClass::DerivedClass(std::string data) : BaseClass(data) { } share|improve this answer answered Jul 23 '10 at 15:56 Zitrax 5,73174258 3 +1. It's stronger than should not. It is actually cannot. –JaredPar Jul 23 '10 at
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 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 http://stackoverflow.com/questions/3319892/c-why-does-my-derivedclasss-constructor-not-have-access-to-the-baseclasss-p programmers, just like you, helping each other. Join them; it only takes a minute: Sign up error: class ‘Software’ does not have any field named ‘ptr’ [closed] up vote 0 down vote favorite I am getting the following errors: softwarew.hh: In constructor ‘Software::Software(std::string, int)’: softwarew.hh:26:45: error: class ‘Software’ does not have any field named ‘ptr’ softwarew.hh:28:7: http://stackoverflow.com/questions/19224255/error-class-software-does-not-have-any-field-named-ptr error: ‘ptr’ was not declared in this scope softwarew.hh: In destructor ‘Software::~Software()’: softwarew.hh:40:6: error: ‘ptr’ was not declared in this scope Can someone explain why I receive these errors? The code that causes the errors: Software(std::string name, int revision) : ptr(software_construct(name.c_str(), revision) ) { if(!ptr) throw std::runtime_error("no software created"); } ~Software(){ if(ptr) software_destruct(ptr); } private: struct Software_s* ptr; c++ share|improve this question asked Oct 7 '13 at 11:55 Spacy 307 closed as off-topic by Roman R., interjay, nijansen, Mark, Benjamin Bannier Oct 7 '13 at 12:21 This question appears to be off-topic. The users who voted to close gave these specific reasons:"Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – interjay, Mark, Benjamin Bannier"Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow que
{ public: int i; explicit Base(int _i) : i(_i) { //other initialization code } }; But we cannot initialize the parent class public or protected member variable in derived class constructor's initialization list. class Derived: public Base { public: int j; Derived(int _i, https://geekwentfreak-raviteja.rhcloud.com/blog/2014/08/05/c-initializing-parent-class-member-variables-in-derived-class-constructor/ int _j) : i(_i), j(_j) { } }; The above code will produce the following error: par_var_init.cpp: In constructor ‘Derived::Derived(int, int)’: par_var_init.cpp:16:28: error: class ‘Derived’ does not have any field named ‘i’ Derived(int _i, int _j) http://www.cplusplus.com/forum/beginner/46039/ : i(_i), j(_j) { Using parent constructor The correct way to do this is by calling the base class constructor to initialize its variables. class Derived: public Base { public: int j; Derived(int _i, int _j) does not : Base(i), j(_j) { } }; Initializing parent's member variables in derived class constructor's body It is possible to initialize parent's member variables in derived constructor's body. class Derived: public Base { public: int j; Derived(int _i, int _j) : j(_j) { i = _i; } }; Note: the following approach is wrong! class Derived: public Base { public: int i; int j; Derived(int _i, int _j) : i(_i), j(_j) { } }; does not have This will create a new variable in derived class called i that is separate from Base::i. tejaineceRavi Teja is a technology entrepreneur with interest in Embedded Systems, Software engineering, Operating Systems, Web development, Cloud computing, Machine learning, etc.More Posts - Website Follow Me: POSTED IN: CTAGGED: base • basics • c • child • constructor • derived • initialize • parent Post navigation vim: avoid selecting and copying line numberC++: shared_ptr implementation for fun Google+ comment widget by skipser Created by tejainece Ravi Teja is a technology entrepreneur with interest in Embedded Systems, Software engineering, Operating Systems, Web development, Cloud computing, Machine learning, etc. View all posts Search for: Recent Posts Qt Creator error: Cannot retrieve debugging output boot2docker: Add volume from windows host to the docker container C: Initialize multiple variables in same declaration statement C: declaring multipe pointer variables in same declaration statement Record memory and processor usage of a particular process using top Tagsarray assignment avconv binding c command constructor copy custom dart debian derived EDA element ffmpeg gate Go Golang interface io level libavcodec line linux Modelsim operator output parent pointer polymer Quartus read shared_ptr simulation String systemverilog template tool ubuntu vector verification Verilog VHDL VLSI width Recent CommentsGet duration of a video using ffmpeg or avconv | GeekWentFreak on I
run is very simple and I dont know what the problem is: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include