Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Drakkcon

Nested Classes/Sub-Classes in C++

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
That will work, but those aren''t nested classes; you merely have one global class as a member of another.

Share this post


Link to post
Share on other sites
meh. Oh, and thanx for the clear up - time for my inner OO control freak to manifest! May there be constructers everywhere!(evil laugh).

Share this post


Link to post
Share on other sites
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 -g3

C:/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 here
C:/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 1

Execution 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!