From: The|Godfather on 24 Oct 2006 18:14 Hi there I have a problem with a program code. As the code is huge and is copyrighted I just simulated the error with some useless code. Here is the full report now: gcc --version: gcc (GCC) 3.4.2 uname -rmo: 2.6.5-7.201-smp x86_64 GNU/Linux compile command: gcc -c -I/users/dstanche/problem test1.cpp Error Message: "test1.cpp: In constructor `test1::StatementInternals::StatementInternals()': test1.cpp:8: error: invalid use of nonstatic data member 'test1::stmt_internals' " CODE : example.h: ------------ class example { public: example() {;} inline int giveIt(){return 2;} }; -------- test1.h ------- #include <example.h> class test1 :example{ class StatementInternals; public: test1(); private: StatementInternals * stmt_internals; }; --------- test1I.h --------- class test1::StatementInternals { public: StatementInternals(); unsigned long *length; bool select_statement; int stmt_counter; }; -------- test1.cpp ------- #include <test1.h> #include <test1I.h> test1::StatementInternals::StatementInternals() : length(0), select_statement(0) { stmt_counter=stmt_internals->giveIt(); // THE PROBLEM LINE IS THIS ONE } test1::test1():stmt_internals(new StatementInternals){} int main() { return 0; } ------- As you can see the code does not do anything special. I have NO idea why the problem occurs. Please advice. Dragomir Stanchev -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alberto Ganesh Barbati on 24 Oct 2006 22:46 The|Godfather ha scritto: > class test1 :example{ > class StatementInternals; > public: > > test1(); > private: > StatementInternals * stmt_internals; > > }; > > <snip> > > -------- > test1.cpp > ------- > #include <test1.h> > #include <test1I.h> > > test1::StatementInternals::StatementInternals() > : length(0), select_statement(0) > > { > stmt_counter=stmt_internals->giveIt(); // THE PROBLEM LINE IS THIS > ONE There are two different errors here. Your compiler is apparently reporting only the first one however. Forget for a moment that StatementInternals is a nested class of test1 and that stmt_internals is private. Would that work? No, because in order to use a non-static member of a class you need to provide an *instance* of such class. For example: void foo(test1* ptr) { stmt_internal; // ERROR: which stmt_internal? ptr->stmt_internal; // OK: THE stmt_internal of *ptr } Your case is the same: you don't have an instance of class test1 to work on and you can't use stmt_internals without providing one. Don't be fooled by the fact that class StatementInternals is nested in class test1. They are still two different classes and there is *no* implicit containment relationship between them. The other mistake is that stmt_internal is a pointer to an object of class test1::StatementInternals, but that class does *not* have a giveIt() member function. giveIt() was defined in class example and so it's inherited by class test1, but StatementInternals does *not* inherit from test1 (nor from example). Morale: nesting doesn't imply neither containment nor inheritance. HTH, Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Base Class undefined but header right above it! Next: Reference to void |