Thread: Trouble connecting ConsolApplication1.cpp, BoxClass.h, BoxClass.cpp

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Question Trouble connecting ConsolApplication1.cpp, BoxClass.h, BoxClass.cpp

    I created a basic consol program that creates a rectangle of the users desired height and width. I wanted to learn how classes worked so I just used one file. Now i'm trying to properly put the class into a .h and .cpp file. I have a whole bunch of errors concerning the variables. Can someone point out what I'm doing wrong and how I should fix it? BTW all my cpp and header files are in the same folder.


    ConsoleApplication1.cpp:

    Code:
    #include "BoxClass.h"
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    
    
    int main() {
        //variable declaration/definition
        int width_Var;
        int height_Var;
        int number_of_Boxes;
    
    
        //object declaration/Body
        cout << "Enter width of rectangle/box\nWidth = ";
        cin >> width_Var;
        cout << "Enter height of rectangle/box\nHeight = ";
        cin >> height_Var;
        cout << "How many rectangles/boxes do you want?\n";
        cin >> number_of_Boxes;
    
    
        BoxClass box1(width_Var, height_Var, number_of_Boxes);
        
        cout <<"Box Area = "<<box1.Rectangle_Area() << endl;
    
    
    //exit
        cout << "\n\n\n\n\n";
        system("pause");
        return 0;
    }
    BoxClass.h:

    Code:
    #ifndef BOXCLASS_H
    #define BOXCLASS_H
    
    
    class BoxClass {
        //prv variables
        unsigned short int width;
        int height, i;
        float space_Value;
        float height_Count;
        bool error;
        //prv functions
        void Print_Rectangle(int x, int y);
    
    
    public:
        //function shows area of individual spaces
        float Rectangle_Area();
        //function shows area of individual spaces
    
    
        // constructor
        BoxClass(int x, int y, int amount);
    };
    
    
    #endif

    BoxClass.cpp:

    Code:
    #include "BoxClass.h"
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    
    
        void BoxClass::Print_Rectangle(int x, int y) {
    
    
            //calc
            space_Value = (3 * x) - 4;
    
    
            //draw top of box
            for (width = 1; width < x; width += 1) {
                cout << "...";
            }
            cout << "\n";
    
    
            //draw sides
            for (height = 1; height < y; height += 1) {
                cout << ":";
                height_Count++;
                for (width = 1; width < space_Value; width += 1) {
                    cout << " ";
                }
    
    
                cout << ":\n";
            }
    
    
            //draw bottom
            cout << ":";
    
    
            for (width = 1; width < space_Value; width += 1) {
                cout << ".";
            }
            cout << ":\n";
        }
    
    
    
    
        //function shows area of individual spaces
        float BoxClass::Rectangle_Area() {
            if (error == false) {
                return (height_Count - .5)*(space_Value - 1);
            }
            else {
                return 0;
            }
        }
    
    
        // constructor
        BoxClass::BoxClass(int x, int y, int amount) {
            float height_Count = 1;
            bool error = false;
    
    
            if (x <= 41) {
                for (i = 1; i <= amount; i += 1) {
                    Print_Rectangle(x, y);
                }
            }
            else {
                error = true;
                cout << "Error - width must be below 42!\n";
            }
        };

    ERRORS:

    1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
    1>BoxClass.cpp
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(7): error C2653: 'BoxClass': is not a class or namespace name
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(10): error C2065: 'space_Value': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(13): error C2065: 'width': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(19): error C2065: 'height': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(21): error C2065: 'height_Count': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(22): error C2065: 'width': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(22): error C2065: 'space_Value': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(32): error C2065: 'width': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(32): error C2065: 'space_Value': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(40): error C2653: 'BoxClass': is not a class or namespace name
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(41): error C2065: 'error': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(42): error C2065: 'height_Count': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(42): error C2065: 'space_Value': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(50): error C2653: 'BoxClass': is not a class or namespace name
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(55): error C2065: 'i': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\boxclass.cpp(63): warning C4508: 'BoxClass': function should return a value; 'void' return type assumed
    1>ConsoleApplication1.cpp
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(21): error C2065: 'BoxClass': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(21): error C2146: syntax error: missing ';' before identifier 'box1'
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(21): error C3861: 'box1': identifier not found
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(23): error C2065: 'box1': undeclared identifier
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(23): error C2228: left of '.Rectangle_Area' must have class/struct/union
    1>c:\users\cade\source\repos\consoleapplication1\c onsoleapplication1\consoleapplication1.cpp(23): note: type is 'unknown-type'
    1>Generating Code...
    1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You did not define BOXCLASS_H in the project settings, correct?

    Because that would likely cause your errors.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Guest
    Guest
    There's nothing inherently wrong with your files/code. I can only assume that something about your IDE project isn't set up properly (I don't use it), but that's not really a C++ issue then.

    The BoxClass::BoxClass() definition shouldn't be terminated by a semi-colon, but that's no deal breaker.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming - Trouble Connecting to HTTP Server
    By Mcdom34 in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-07-2015, 12:09 AM
  2. Connecting With IP
    By Phanster in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2004, 08:01 PM
  3. Connecting to a FTP with C#???
    By gicio in forum C# Programming
    Replies: 3
    Last Post: 12-12-2002, 05:10 PM
  4. Connecting to COM port
    By Unimatrix139 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-25-2002, 11:00 AM

Tags for this Thread