By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Access variable in "main.cpp" from other classes

P: 88
Hey, How do you access a variable in main.cpp from a class?

So if I want to get access to "board" how would I do so?

Expand|Select|Wrap|Line Numbers
  1.  
  2. main.cpp:
  3.  
  4. Boxes board[10];
  5.  
  6. Boxes.h: 
  7.  
  8. void displayBox(int instance);
  9.  
  10. Boxes.cpp:
  11.  
  12. void displayBox(int instance)
  13. {
  14.     glBegin(GL_POLYGON);
  15.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)),                (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)));
  16.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize, (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)));
  17.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize, (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize);
  18.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)),                (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize);
  19.     glEnd();
  20. }
  21.  
  22.  
Aug 31 '16 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I may not have this right but it looks like color is a property of Boxes. So I added a color property. See boxes.h below.

This is the main();

Expand|Select|Wrap|Line Numbers
  1. #define  _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. //main.cpp:
  9.  
  10. #include "Boxes.h"
  11. #include "func.h"
  12.  
  13. int main()
  14. {
  15.  
  16.     Boxes board[10];
  17.  
  18.     DisplayBox(board[3]);
  19.  
  20. }
  21.  
The compiler will need to know about Boxes and the DisplayBox function. So there is a Boxes.h and a func.h.

Expand|Select|Wrap|Line Numbers
  1. // Boxes.h
  2. #ifndef BOXES
  3. #define BOXES
  4. class Boxes
  5. {
  6.     int color;
  7. public:
  8.  
  9.     void SetColor(int arg) { this->color = arg; }
  10.     int  GetColor() { return this->color; }
  11.  
  12.  
  13.  
  14. };
  15. #endif
  16.  
  17. //func.h
  18.  
  19. #ifndef FUNCH
  20. #define FUNCH
  21.  
  22. #include "Boxes.h"
  23. void DisplayBox(Boxes& arg);
  24.  
  25. #endif
  26.  
Finally the functions are in their own file but here the compiler needs to know about Boxes:


Expand|Select|Wrap|Line Numbers
  1. // func.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #include "Boxes.h"
  6.  
  7. void DisplayBox(Boxes& arg)
  8. {
  9.     cout << arg.GetColor();
  10. }

Is this what you had in mind?
Sep 1 '16 #2

Oralloy
Expert 100+
P: 983
You can just write this into boxes.cpp
Expand|Select|Wrap|Line Numbers
  1. extern Boxes board[10];
Sep 2 '16 #3

P: 88
Didn't work Oralloy.

Sorry, I forgot to add "float color[3] = {1.0f, 1.0f, 1.0f};" to Boxes.h. I just moved the functions I needed "board[]" in to the "main.cpp" file. Here is my entire code with a separate problem...

In this project I make a window with some squares. When you click on one of these squares it should toggle it's color. BUT for some reason it wont change color, but it registered and found out that I clicked on it...

My guess is that it's an instance flaw or "color[3]" (in Boxes.h) is a global value, which it shouldn't be because it isn't static.

What is causing this flaw?

main.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include "Boxes.h"
  4.  
  5. LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  6. void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
  7. void DisableOpenGL(HWND, HDC, HGLRC);
  8.  
  9. const int wWidth = 1080 + Boxes::space;
  10. const int wHeight = 724 + Boxes::space;
  11. const int nrBoxX = 2; //max = 45
  12. const int nrBoxY = 2; //max = 30
  13. const int nrBoxT = 4; //max = 1350
  14. Boxes board[4];
  15. POINT mousePt;
  16. int mouseX = 0;
  17. int mouseY = 0;
  18. bool isBoxesSetup = false;
  19.  
  20. void setupBox()
  21. {
  22.     int a = 0;
  23.     for (int y = 0; y < nrBoxY; y++)
  24.     {
  25.         for (int x = 0; x < nrBoxX; x++)
  26.         {
  27.             board[a].xPosition = x;
  28.             board[a].yPosition = y;
  29.             a++;
  30.         }
  31.     }
  32.     isBoxesSetup = true;
  33. }
  34.  
  35. void displayBox(int instance)
  36. {
  37.     glBegin(GL_POLYGON);
  38.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)),                (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)));
  39.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize, (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)));
  40.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize, (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize);
  41.         glColor3f(board[instance].color[0], board[instance].color[1], board[instance].color[2]); glVertex2i((int) Boxes::space + (board[instance].xPosition * (Boxes::bSize + Boxes::space)),                (int) Boxes::space + (board[instance].yPosition * (Boxes::bSize + Boxes::space)) + Boxes::bSize);
  42.     glEnd();
  43. }
  44.  
  45. int WINAPI WinMain(HINSTANCE hInstance,
  46.                    HINSTANCE hPrevInstance,
  47.                    LPSTR lpCmdLine,
  48.                    int nCmdShow)
  49. {
  50.     WNDCLASSEX wcex;
  51.     HWND hwnd;
  52.     HDC hDC;
  53.     HGLRC hRC;
  54.     MSG msg;
  55.     BOOL bQuit = FALSE;
  56.  
  57.     /* register main window class */
  58.     wcex.cbSize = sizeof(WNDCLASSEX);
  59.     wcex.style = CS_OWNDC;
  60.     wcex.lpfnWndProc = WindowProc;
  61.     wcex.cbClsExtra = 0;
  62.     wcex.cbWndExtra = 0;
  63.     wcex.hInstance = hInstance;
  64.     wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  65.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  66.     wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  67.     wcex.lpszMenuName = NULL;
  68.     wcex.lpszClassName = "All White";
  69.     wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  70.  
  71.  
  72.     if (!RegisterClassEx(&wcex))
  73.         return 0;
  74.  
  75.     /* create main window */
  76.     hwnd = CreateWindowEx(0,
  77.                           "All White",
  78.                           "All White",
  79.                           WS_OVERLAPPEDWINDOW,
  80.                           CW_USEDEFAULT,
  81.                           CW_USEDEFAULT,
  82.                           wWidth,
  83.                           wHeight,
  84.                           NULL,
  85.                           NULL,
  86.                           hInstance,
  87.                           NULL);
  88.  
  89.     ShowWindow(hwnd, nCmdShow);
  90.  
  91.     /* enable OpenGL for the window */
  92.     EnableOpenGL(hwnd, &hDC, &hRC);
  93.  
  94.     /* program main loop */
  95.     while (!bQuit)
  96.     {
  97.         /* check for messages */
  98.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  99.         {
  100.             /* handle or dispatch messages */
  101.             if (msg.message == WM_QUIT)
  102.             {
  103.                 bQuit = TRUE;
  104.             }
  105.             else
  106.             {
  107.                 TranslateMessage(&msg);
  108.                 DispatchMessage(&msg);
  109.             }
  110.         }
  111.         else
  112.         {
  113.             /* OpenGL animation code goes here */
  114.  
  115.             glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  116.             glClear(GL_COLOR_BUFFER_BIT);
  117.  
  118.             glPushMatrix();
  119.  
  120.             glOrtho(0, wWidth, wHeight, 0, -1, 1);
  121.  
  122.             if (!isBoxesSetup) setupBox();
  123.  
  124.             for (int a = 0; a < nrBoxT; a++)
  125.             {
  126.                 displayBox(a);
  127.             }
  128.  
  129.             glPopMatrix();
  130.  
  131.             SwapBuffers(hDC);
  132.         }
  133.     }
  134.  
  135.     /* shutdown OpenGL */
  136.     DisableOpenGL(hwnd, hDC, hRC);
  137.  
  138.     /* destroy the window explicitly */
  139.     DestroyWindow(hwnd);
  140.  
  141.     return msg.wParam;
  142. }
  143.  
  144. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  145. {
  146.     switch (uMsg)
  147.     {
  148.         case WM_CLOSE:
  149.             PostQuitMessage(0);
  150.         break;
  151.  
  152.         case WM_DESTROY:
  153.             return 0;
  154.         break;
  155.  
  156.         case WM_LBUTTONDOWN:
  157.             GetCursorPos(&mousePt);
  158.             ScreenToClient(hwnd, &mousePt);
  159.             mouseX = mousePt.x;
  160.             mouseY = mousePt.y;
  161.             for (int i = 0; i < nrBoxT; i++)
  162.             {
  163.                 bool buttonPressed = board[i].checkIfPressed(mouseX, mouseY);
  164.                 if (board[i].checkIfPressed(mouseX, mouseY))
  165.                 {
  166.                     board[i].toggleColorState(i);
  167.                 }
  168.             }
  169.         break;
  170.  
  171.         case WM_KEYDOWN:
  172.         {
  173.             switch (wParam)
  174.             {
  175.                 case VK_ESCAPE:
  176.                     PostQuitMessage(0);
  177.                 break;
  178.  
  179.                 case VK_SPACE:
  180.                     PostQuitMessage(0);
  181.                 break;
  182.             }
  183.         }
  184.         break;
  185.  
  186.         default:
  187.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  188.     }
  189.  
  190.     return 0;
  191. }
  192.  
  193. void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
  194. {
  195.     PIXELFORMATDESCRIPTOR pfd;
  196.  
  197.     int iFormat;
  198.  
  199.     /* get the device context (DC) */
  200.     *hDC = GetDC(hwnd);
  201.  
  202.     /* set the pixel format for the DC */
  203.     ZeroMemory(&pfd, sizeof(pfd));
  204.  
  205.     pfd.nSize = sizeof(pfd);
  206.     pfd.nVersion = 1;
  207.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  208.                   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  209.     pfd.iPixelType = PFD_TYPE_RGBA;
  210.     pfd.cColorBits = 24;
  211.     pfd.cDepthBits = 16;
  212.     pfd.iLayerType = PFD_MAIN_PLANE;
  213.  
  214.     iFormat = ChoosePixelFormat(*hDC, &pfd);
  215.  
  216.     SetPixelFormat(*hDC, iFormat, &pfd);
  217.  
  218.     /* create and enable the render context (RC) */
  219.     *hRC = wglCreateContext(*hDC);
  220.  
  221.     wglMakeCurrent(*hDC, *hRC);
  222. }
  223.  
  224. void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
  225. {
  226.     wglMakeCurrent(NULL, NULL);
  227.     wglDeleteContext(hRC);
  228.     ReleaseDC(hwnd, hDC);
  229. }
  230.  
Boxes.h:
Expand|Select|Wrap|Line Numbers
  1. #ifndef BOXES_H
  2. #define BOXES_H
  3.  
  4. class Boxes
  5. {
  6.     public:
  7.         float color[3] = {1.0f, 1.0f, 1.0f};
  8.         const static int space = 4;
  9.         const static int bSize = 20;
  10.         int xPosition = 0;
  11.         int yPosition = 0;
  12.         int realMinX = (int) space + (xPosition * (bSize + space));
  13.         int realMaxX = (int) space + (xPosition * (bSize + space)) + bSize;
  14.         int realMinY = (int) space + (yPosition * (bSize + space));
  15.         int realMaxY = (int) space + (yPosition * (bSize + space)) + bSize;
  16.         int cState = 3;
  17.         int nState = 0;
  18.  
  19.         Box(int xPos, int yPos);
  20.         float getColor();
  21.         void setColor(float red, float green, float blue);
  22.         void toggleColorState(int i);
  23.         bool checkIfPressed(int x, int y);
  24.  
  25.     protected:
  26.  
  27.     private:
  28.  
  29. };
  30.  
  31. #endif // BOXES_H
  32.  
  33.  
Boxes.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "Boxes.h"
  2. #include <windows.h>
  3. #include <gl/gl.h>
  4.  
  5. extern Boxes board[4];
  6.  
  7. Boxes::Box(int xPos, int yPos)
  8. {
  9.     xPosition = xPos;
  10.     yPosition = yPos;
  11. }
  12.  
  13. void Boxes::toggleColorState(int i)
  14. {
  15.     cState = nState;
  16.     nState++;
  17.     if (nState > 3) nState = 0;
  18.  
  19.     if (cState == 0)
  20.     {
  21.         board[i].color[0] = 1.0f;
  22.         board[i].color[1] = 0.0f;
  23.         board[i].color[2] = 0.0f;
  24.     }
  25.     if (cState == 1)
  26.     {
  27.         board[i].color[0] = 0.0f;
  28.         board[i].color[1] = 1.0f;
  29.         board[i].color[2] = 0.0f;
  30.     }
  31.     if (cState == 2)
  32.     {
  33.         board[i].color[0] = 0.0f;
  34.         board[i].color[1] = 0.0f;
  35.         board[i].color[2] = 1.0f;
  36.     }
  37.     if (cState == 3)
  38.     {
  39.         board[i].color[0] = 1.0f;
  40.         board[i].color[1] = 1.0f;
  41.         board[i].color[2] = 1.0f;
  42.     }
  43. }
  44.  
  45. //Not used
  46. float Boxes::getColor()
  47. {
  48.     return *color;
  49. }
  50.  
  51. //Not used
  52. void Boxes::setColor(float red, float green, float blue)
  53. {
  54.     color[0] = red;
  55.     color[1] = green;
  56.     color[2] = blue;
  57. }
  58.  
  59. bool Boxes::checkIfPressed(int x, int y)
  60. {
  61.     if (x > realMinX && x < realMaxX && y > realMinY && y < realMaxY) return true;
  62.     return false;
  63. }
  64.  
  65.  
Sep 2 '16 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
You cannot put any code in a header file that creates variables or that is code for the body of a function. Every time the .h is included by the various .cpp files you get another set of variables. Then your build dies in the link with redefinition errors.

Instead use a #define in your include:

Expand|Select|Wrap|Line Numbers
  1. #define RED 1.0
  2. #define BLUE  2.0
  3.  
There should never be any hard-coded symbolic constants in your code. A symbolic constant is like 4 or 25.2, etc...

What is this?????

Expand|Select|Wrap|Line Numbers
  1. void Boxes::toggleColorState(int i)
  2. 14. {
  3. 15.     cState = nState;
  4. 16.     nState++;
  5. 17.     if (nState > 3) nState = 0;
  6. 18. 
  7. 19.     if (cState == 0)
  8. 20.     {
  9. 21.         board[i].color[0] = 1.0f;
  10. 22.         board[i].color[1] = 0.0f;
  11. 23.         board[i].color[2] = 0.0f;
  12. 24.     }
  13.  
This is class member function. There is no board variable possible. board is an array in main(). There is no variable i The code I sent you uses board only in main().

Also, your class should have no public data members. This is not a C struct but a C++ class. Any color values should be defined in the header file as above, or you will need a Color class that has a private obj in Boxes.

Remember, all the processing is in main(). All the class member functions do is manage the private data members of the class.

It looks like you are merging the application code with the class code and this will never work.

Expand|Select|Wrap|Line Numbers
  1. main.cpp:
  2.  
  3. Boxes board[10];
  4.  
  5.       board[1].setColor(etc...};
This code changes the Boxes object board[1]. Inside the member function all you have is the address of the board object which is calling the member function. Read up on the this pointer.

keep posting . We have all gone through this.
Sep 3 '16 #5

Expert 100+
P: 2,396
It isn't that you can't put variable or function definitions in a header file -- it's that it is almost always a mistake to do so. Sometimes doing so triggers a build error, other times it may yield a possibly intermittent run-time bug.

Typically, you put a variable or function definition in one .c/.cpp file and the corresponding declaration in a header file that is included by all .c/.cpp files that define or refer to that variable or function.
Sep 7 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.