Nested Classes/Sub-Classes in C++

Started by
12 comments, last by dotnetravi 19 years, 10 months ago
How do you nest classes in C++. Why doesn''t my code work?:

#include <iostream>

using namespace std;

class foo
{
 public:
 class jimbo
 {
  public:
  int wassup = 74;
 };
};

foo w;

int main()
{
 cout << w.jimbo.wassup;
 return 0;
}
The true general first seeks victory, then seeks battle - Sun Tzu
Advertisement
Okay, your "class foo { ... };" describes a foo class, but it does not create a foo object. To do that you had to create one with "foo w;" Similarly, your jimbo class in foo is being defined (accessable via foo::jimbo), but there is no actual object of type "jimbo" inside the foo class.
Ohhh, so they should be separate. Like this:
#include <iostream>using namespace std;struct jimbo //I''m too lazy to type public{ int wassup = 47;};struct foo //might as well be struct{ jimbo nested;}parent;int main(){  cout << parent.nested.wassup;  return 0;}
That will work, but those aren''t nested classes; you merely have one global class as a member of another.
meh. Oh, and thanx for the clear up - time for my inner OO control freak to manifest! May there be constructers everywhere!(evil laugh).
nested class:

class A{public:    class B    {    public:        int foo;    } nestedB;};
it doesn't work:

error:
g++.exe -D__DEBUG__ -c Main.cpp -o Main.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/DX9bSDK/Include"   -ansi -fsave-memoized -O3 -pg -g3C:/Dev-Cpp/include/c++/bits/locale_facets.tcc: In function `int    std::__convert_from_v(char*, int, const char*, _Tv, int* const&, int) [with    _Tv = long unsigned int]':C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:720:   instantiated from `_OutIter std::num_put<_CharT, _OutIter>::_M_convert_int(_OutIter, std::ios_base&, _CharT, char, char, _ValueT) const [with _ValueT = long unsigned int, _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:899:   instantiated from `_OutIter std::num_put<_CharT, _OutIter>::do_put(_OutIter, std::ios_base&, _CharT, long unsigned int) const [with _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'C:/Dev-Cpp/include/c++/bits/locale_facets.h:745:   instantiated from `_OutIter std::num_put<_CharT, _OutIter>::Put(_OutIter, std::ios_base&, _CharT, long unsigned int) const [with _CharT = char, _OutIter = std::ostreambuf_iterator<char, std::char_traits<char> >]'C:/Dev-Cpp/include/c++/bits/ostream.tcc:215:   instantiated from `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]'C:/Dev-Cpp/include/c++/ostream:122:   instantiated from `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]'Main.cpp:19:   instantiated from hereC:/Dev-Cpp/include/c++/bits/locale_facets.tcc:2070: `strdup' undeclared (first    use this function)C:/Dev-Cpp/include/c++/bits/locale_facets.tcc:2070: (Each undeclared identifier    is reported only once for each function it appears in.)make.exe: *** [Main.o] Error 1Execution terminated  


#include <iostream>using namespace std;class A{public:        class B    {    public:        int foo;    } nestedB;}whatever;int main(){  whatever.nestedB.foo = 400;  cout << whatever.nestedB.foo;  return 0;}   


It IS a C++ project, and I can think of nothing else.

EDIT: It's a bug in MinGW and Dev-C++.

[edited by - Drakkcon on May 19, 2004 7:43:54 AM]

edit: breaking tables

[edited by - SiCrane on May 19, 2004 5:14:19 PM]
struct jimbo{  int wassup = 47;};

Is not valid C++ code. You can't initialize an non-const non-static datamember like that. You'd have to either initialize the the value in the constructor of jimbo, or make wassup a static const datamember. In the case of the latter solution, you'd access the value like jimbo::wassup.

edit: nm you fixed that problem in your last post

[edited by - Polymorphic OOP on May 19, 2004 10:40:28 AM]
Weed!
class Outside{    public:    Outside() : X(1) {}    int X;        class Inside    {        public:        Inside() : Y(2) {}        int Y;    };};int main(){    Outside outside;    Outside::Inside inside;        cout << outside.X; // 1    cout << inside.Y;  // 2        return 0;}


--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net
I have no idea what "Inside() : Y(2) {}" means? Weed? What?

This topic is closed to new replies.

Advertisement