Class Not Being Recognized/Won't Declare Objects

I created a class 'Rectangle' under the header file 'Rectangle.h' as part of a build that I am doing. However, when I try and declare an object 'box' using that class it doesn't seem to recognize Rectangle as a class that I can make an object with. Also, the code for the header file and class function are in different files that are part of my build but I didn't include them here to prevent clutter. Any thoughts on why this is happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdlib>      // for rand function (random number generator)
#include <fstream>      // for file input and output
#include <iostream>     // for cin and cout
#include <iomanip>      // for manipulators such as setw
#include <string>       // for using string data types
#include <ctime>        // for time() function
#include "Rectangle.h"  // for rectangle function
#include "windows.h"    // for cursor manip

using namespace std;

void placeCursor(HANDLE, int, int);

struct rectDim
{
    double length;
    double width;
};

void displayPrompts(HANDLE);
void getUserInput(HANDLE, rectDim&);
void displayData (HANDLE, rectDim);

int main()
{

  {
    
    // Program Main Menu
    // Projects 1-8 preceded project 9 but are not icluded
    cout <<"9) Project 9.0 - Rectangle Area" << endl;

    switch (num) // switch to programs selected from main menu

    {
      case 9:
            //initialize arrays
            menuChoices[choices].optionNum = 9;
            menuChoices[choices].optionNam = "Project 9.0 - Rectangle Area";

            {
                rectDim input;

                HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

                displayPrompts(screen);
                getUserInput(screen, input);
                displayData (screen, input);
                Rectangle box;             // Declare a Rectangle object
            //program header
            cin.clear();
            system("cls");
            displayPrompts(screen);
            getUserInput(screen, input);

            {
                // Call member functions to get box information to display
                cin.clear();
                system("cls");
                displayData (screen, input);
            }

            system("cls");
            break;//end switch and return to menu

            }
Last edited on
may need to see the .h file.
what is the exact error message of the first error in the code?
You also include windows.h, so maybe it's a name clash.
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-rectangle

Maybe make your rectangle a more unique name.
We need to see your .h files.
> I didn't include them here to prevent clutter.
hey, my car doesn't start so here's the front wheel, ¿can you fix it?


1
2
3
4
class Rectangle{};
int main(){
  Rectangle box;
}
that's without the clutter
I was about to upload my .h file here when I found out there is a conflict with a C++ library function that also has a variable named "Rectangle". I did not expect this. I've updated my files to 'RectangleShape.h' and 'RectangleShape.cpp' and that seems to have fixed the issue. Thank you all.
bbailey wrote:
I was about to upload my .h file here when I found out there is a conflict with a C++ library function that also has a variable named "Rectangle".


So now you have discovered a prime reason for putting your own code into it's own namespaces :+)

While you are at it, get rid of L10, write std:: before each std thing. There is lots written as to why this is a good idea.

Good luck and seasons greetings :+)
The conflict is not with a C++ library function - but within windows.h. It has a Rectangle() function. https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-rectangle

Topic archived. No new replies allowed.