-2

I'm required to work with multiple classes in one .cpp file. I'm not sure what I'm doing wrong, is there a header file I need to include?

These are the errors I'm getting:

error C3646: 'loginObject': unknown override specifier

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2065: 'loginObject': undeclared identifier
#include <iostream>
#include <string>
using namespace std;

//Declaration of Class that Allows User to Select a Menu Option
class Menu
{
private:
    int userChoice = 0; //User's Menu Selection

    //Class Objects
    Login loginObject;

public:
    void options()
    {
        //Loop of Initial Menu
        do
        {
            //Startup Menu
            cout << "\n\tMain Menu:"
                "\n\n\t1. Login"
                "\n\t2. Create Password"
                "\n\t3. Change Password"
                "\n\t4. Delete User"
                "\n\t5. Exit"
                "\n\nSelection: ";
            cin >> userChoice;
            cout << endl;

            //Switch Statement that Determines Course of Action based upon User's Choice
            switch (userChoice)
            {
            case 1:
                loginObject.login();    //Option to Login
                break;
            case 2:
                cout << "\nCreating Password...";  //Option to Create a New Password
                cout << endl;
                break;
            case 3:
                cout << "Changing Password...";  //Option to Change an Old Password
                cout << endl;
                break;
            case 4:
                cout << "Deleting User..."; //Option to Delete a User
                cout << endl;
                break;
            case 5:
                cout << "Exiting...";   //Option to Exit the Program
                cout << endl << endl;
                system("pause");
            default:
                cout << "INVALID MENU OPTION!\n";
                break;
            }

        } while (userChoice != 5); //Loop to Return to Initial Menu
    }
};

//Declaration of Class that Manages all Activities Dealing with Login
class Login
{
private:

public:
    void login()
    {
        cout << "\nLogging In...";
        cout << endl;
    }
};

//Main File
int main()
{
    //Call to Menu Class 
    Menu menuVariable;
    menuVariable.options();

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
smirand
  • 25
  • 4
  • Mind if I ask what compiler you're using? Those are some pretty crummy error messages. gcc gives me the much more helpful (and correct) "error: ‘Login’ does not name a type". Is it an old compiler? – Silvio Mayolo Nov 30 '22 at 20:19
  • I'm using Visual Studio – smirand Nov 30 '22 at 20:22
  • And not looking at the full error messages, just a summary. – sweenish Nov 30 '22 at 21:05
  • Not clear it's the source of the message you're seeing, but having `class login` contain a function named `login` that attempts to return `void` is a problem--a function with the same name as its containing class is a constructor, for which you're not allowed to specify a return type. – Jerry Coffin Nov 30 '22 at 21:33

1 Answers1

1

Generally speaking, your C++ code is read from top to bottom. So you can't use the name Login before it's defined. In your case, you can just move Login to above Menu (since the former does not depend on the latter) and it'll all be fine. In general, you can write a prototype at the top of your file to indicate the shape of a class without showing its implementation. So, for Login, that would be

class Login {
public:
    void login(); // Note: No function body
};
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • WOW i'm so over myself. – smirand Nov 30 '22 at 20:23
  • I moved the login class to above the menu class and it worked, thanks for that clarification Silvio – smirand Nov 30 '22 at 20:24
  • But also note that `login::login` has to be a ctor, which can't specify a return type. – Jerry Coffin Nov 30 '22 at 21:34
  • @JerryCoffin That function doesn't look like a constructor to me. Its implementation prints some text out and (presumably) changes some internal state. At no point does it attempt to construct an instance of `Login`. – Silvio Mayolo Nov 30 '22 at 23:25
  • @SilvioMayolo: true--but since it's named `login` and its contained in a class named `login`, it can't possibly be anything else. A member function named the same as the class that contains it must always be a constructor. – Jerry Coffin Dec 01 '22 at 00:40
  • @JerryCoffin Check the capitalization on both. In both OP's code and mine, it's a method named `login` in a class called `Login` (keeping with the way C++ programmers typically name classes), and such a method is perfectly cromulent. Normally, I might object to having such similar names for a class and a function, but "login" in English is both a noun and a verb. I can have a `Login` (i.e. my user account), to which I attempt to `login` (meaning provide authentication details for), so this seems like a legitimate naming scheme to me. – Silvio Mayolo Dec 01 '22 at 00:41
  • @SilvioMayolo: ah, good point on capitalization--my mistake. I'm less convinced by the argument in favor of the naming though. If `Login` is intended to mean `UserAccount`, then `UserAccount` would be a much better name for it. – Jerry Coffin Dec 01 '22 at 00:48