LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2009, 05:14 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
class within a class in C++


SOLVED sorry can't delete


Hi all,
I'm a fairly advanced C programmer but have little experience in C++, and need to convert some code to C++, basically I have 3 "nested classes" but am having trouble calling a variable from the "inner class".
Basically "main" class is MD5ModelLoader, then I have a class for mesh

class Mesh
{
public:
std ::vector<Texture> textures;
std ::vector<vertecies> verts;
//etc.......

};

and a texture class

class Texture
{
public:
std::string texture_name;



}


so if I want to display texture string I do

mesh->textures.texture_name=shader_from_file;

printf("shader name %s \n",mesh->texture.texture_name.c_str());

This gives me
error : 'class std :: vector<MD5ModelLoader::Texture, std::<MD5ModelLoader::Texture> >' has no member named 'texture_name'

It looks like it's not seeing mesh class, but I know it is because the vettecies class works fine :s

normally I'd do these as structures but need to use C++, can anyone advise. Thanks.

Last edited by knobby67; 07-21-2009 at 05:18 AM.
 
Old 07-21-2009, 05:42 PM   #2
pilotmm
LQ Newbie
 
Registered: Aug 2007
Distribution: openSuse
Posts: 26

Rep: Reputation: 2
Forgive me for my post, I didn't realize you found a solution. None the less, this will give a solution to other confused readers.

Hi knobby67

The problem is with this line:

Quote:
mesh->textures.texture_name=shader_from_file;
Let me explain how this compiles:

1) ``mesh->textures'' compiles into the ``textures'' within Mesh which is of type ``std::vector<Texture>''
2) Then the compiler attempts to access ``.texture_name'' from that object.

Problem is that since this is of type ``std::vector<Texture>'', not type ``Texture'' the compiler doesn't know what you are reffering to and throws an error. instead of ``.texture_name'' you need to select what ``Texture'' you want within textures.

Translating into C talk:

Code:
struct texture{
   char* texture_name;
};

struct Mesh{
   texture* textures;
};

main()
{
    struct Mesh *mesh;

    mesh=(struct Mesh *)malloc(sizeof(struct Mesh);
    mesh->textures=(texture*)malloc(N*sizeof(texture))

    mesh->textures.texture_name=shader_from_file;    // WRONG!!
    mesh->textures[n].texture_name=shader_from_file; // Right
}
Does that make sense?

Good luck
pilotmm

Last edited by pilotmm; 07-21-2009 at 07:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration