I can't understand why this piece of code (simplified example) would produce an error:
Error message with MingW:Code:#include <string> class B { public: B(const std::string& s); protected: std::string str; std::string modified_str; }; class D: public B { public: D(const std::string& s); }; B::B(const std::string& s): str(s) {} D::D(const std::string& s): B(s), modified_str(str) { //modified_str = str; } int main(){}
MS VC 2005 Express compiler also mentions that neither D nor B has a member called modified_str.D:\Untitled1.cpp: In constructor `D::D(const std::string&)':
D:\Untitled1.cpp:21: error: class `D' does not have any field named `modified_str'
Trying to use the scope operator produces a different error (expecting class name before "(").
However, the commented out code compiles nicely.
(In real use I won't know what B::str will be before the base constructor finishes. At this point it is up to the derived classes to decide how to initialize modified_str - some would call another function to mutate str first, other would leave it as it is.)



LinkBack URL
About LinkBacks



