accessing/declare protected class in class

This is a discussion on accessing/declare protected class in class within the C++ Programming forums, part of the General Programming Boards category; Hi, I got a little problem over here and i like to have an advice, what i am doing wrong. ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    accessing/declare protected class in class

    Hi,

    I got a little problem over here and i like to have an advice, what i am doing wrong.
    This is an example:

    myMainClass.h
    Code:
    #include myClass.h
    public class myMainClass
    {
     public :
      //Constructor
      myMainClass();
     protected:
      myClass mc;
    }
    myMainClass.cc
    Code:
    #include myMainClass.h
    //Constructor
    myMainClass()
    {
     mc::setOptions(...);
    }
    myClass.h
    Code:
    public class myClass
    {
     public:
     //Constructor
     myClass();
     //method
     void setOptions(...);
    }
    I want to declare myClass in myMainClass as protected member, so i can use myClass in myMainClass freely (in all methods there).

    When i try to compile my project i get that mc "is not a class or namespace". So where is my error?
    Thanks for your time

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You have to put a semicolon after class declarations!

    Code:
    class myClass
    {
    // do stuff
    };
    Edit: Do you want to inherit (protected)? Then you should do something like:

    Code:
    class myClass : protected myMainClass
    {
    // do stuff
    };
    Last edited by Ideswa; 06-28-2007 at 03:19 AM.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Code:
    mc::setOptions(...);
    This should be:

    Code:
    mc.setOptions(...);
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    Code:
    #include "myMainClass.h"
    //Constructor
    myMainClass::myMainClass()
    {
        ...
    }
    Last edited by hk_mp5kpdw; 06-28-2007 at 11:00 AM. Reason: Added quotes around header name
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't confuse classes and objects. Classes are the blueprints, objects the individual instances.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing a protected member declared in parent class
    By Canadian0469 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 02:50 PM
  2. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  3. how to access "private or protected " in Class
    By johnnypiere in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2002, 01:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21