Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with public variables

Problem with public variables

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jw81
    wrote on last edited by
    #1

    I declared a variable through ClassView>Add Member Variable in my Cview. I included header in my MainFrm.cpp. How to use the variable because it doesnt recognise my variable. Do I need to make an object inside my MainFrm.cpp? I tried to use CProgView cview; cview.myvariable; but it says cannot access protected member declared in class. Thanks

    B 1 Reply Last reply
    0
    • J jw81

      I declared a variable through ClassView>Add Member Variable in my Cview. I included header in my MainFrm.cpp. How to use the variable because it doesnt recognise my variable. Do I need to make an object inside my MainFrm.cpp? I tried to use CProgView cview; cview.myvariable; but it says cannot access protected member declared in class. Thanks

      B Offline
      B Offline
      bmzhao
      wrote on last edited by
      #2

      Add Member Variable with 'public',not 'protect', than you can access it.

      J 1 Reply Last reply
      0
      • B bmzhao

        Add Member Variable with 'public',not 'protect', than you can access it.

        J Offline
        J Offline
        jw81
        wrote on last edited by
        #3

        It is public variable from CView. I use it in CDoc, how to declare it? THanks

        J 1 Reply Last reply
        0
        • J jw81

          It is public variable from CView. I use it in CDoc, how to declare it? THanks

          J Offline
          J Offline
          jw81
          wrote on last edited by
          #4

          Thx Astham There are 3 kinds of member variables/functions: public: Can be used/called by objects of any class private: Can be used by objects of this class only protected: Can be used by objects of this class and derived classes only. So if you want to use the variable from somewhere else than a CProgView member function, you need to make the variable public. If you use Add Member Variable there's three radio buttons at the bottom of the dialog. Or you could just write public: at the line before the variable declaration. THis is my header code,

          // Implementation
          public:
          float pointX[100];
          float pointY[100];
          virtual ~CMyProgView();
          #ifdef _DEBUG
          virtual void AssertValid() const;
          virtual void Dump(CDumpContext& dc) const;
          #endif

          ] This is my public variable declaration. Why cant I use it? How to use it from my CMyProgDoc? I have #include CMyProgView.h and use it such as below: CMyProgView obView; obView.pointX[i] = something;

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups