Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to declare and access method functions from different class files

So I’m writing a POS system for a project at school and I’m having trouble declaring the call method for each file to access the said methods

main.cpp

#include <iostream>
#include <windows.h>
int main()
{
    AppUI UI;
    SecuritySys SecSysFunc;
    EditBook BookFunc;

    UI.MainMenu();
}

AppUI.cpp

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>

#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"

AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;

using namespace std;

void AppUI::MainMenu()
{
    //variable declarations
    int opt;

    MenuStart:

    system("CLS");
    TitleHeader();
    setTxtColor(10);
    PageTitle("Main Menu");
    string Menu[] = {"Add Books", "Search Books", "View Books", "Delete Book", "Exit"};
    cout << "1.\tAdd Books" << endl;
    cout << "2.\tSearch Books" << endl;
    cout << "3.\tView Books" << endl;
    cout << "4.\tDelete Books" << endl << endl;
    cout << "0.\tExit";

    cout << "\n\nEnter Option: ";
    cin >> opt;

    switch(opt)
    {
        case 1:
            BookFunc.AddBook();
            break;

        case 2:
            BookFunc.AddBook();
            break;

        case 3:
            BookFunc.AddBook();
            break;

        case 4:
            BookFunc.AddBook();
            break;

        case 0:
            SecSysFunc.Logout();
            break;

        default:
            cout << "Invalid option!";
            sleep(1);
            goto MenuStart;
    }
}

AppUI.h

#ifndef APPUI_H
#define APPUI_H


class AppUI
{
    public:
        void MainMenu();
        void AddBook();
        void SearchBook();

    private:
};

#endif // APPUI_H

EditBook.cpp

#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>

#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"

AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;

using namespace std;

void EditBook::AddBook()
{
    system("CLS");
    UI.AddBook();
}

SearchByTitle(string searchTxt)
{
    cout << "Enter search by title code here";
    system("pause");
    UI.MainMenu();
}

EditBook.h

#ifndef EDITBOOK_H
#define EDITBOOK_H


class EditBook
{
    public:
        void AddBook();

        void SearchBook();

        void ViewBooks();

        void DeleteBooks();

    protected:

    private:
};

#endif // EDITBOOK_H

SecuritySys.cpp

#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>

#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"

AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;

using namespace std;
void SecuritySys::Login()
{
    UI.MainMenu();
}

void SecuritySys::Logout()
{
    exit(0);
}

SecuritySys.h

#ifndef SECURITYSYS_H
#define SECURITYSYS_H


class SecuritySys
{
    public:
        void Login();
        void Logout();

    private:

};

#endif

When I add AppUI UI; SecuritySys SecSysFunc; EditBook BookFunc; to the top of AppUI.cpp EditBook.cpp and SecuritySys.cpp I get an error saying multiple definition of UI,SecSysFunc and the BookFunc. But if I don’t add them at the top, then the BookFunc.AddBook(); for example, the method can’t be recognized and therefor can’t be called or used. What am I doing wrong here?

>Solution :

You are getting a multiple definition error because you are just redeclaring the objects UI, SecSysFunc and BookFunc. You need to make it clear to the compiler that these identifiers point to the same object. You can do so by picking one of the four declarations to be the definition, leave those lines as is. But to the declarations in other code files add extern before the declaration. This will tell the compiler to look in other linked object files for the definition of those objects

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading