简体   繁体   English

如何从不同的 class 文件声明和访问方法函数

[英]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所以我正在为学校的一个项目编写一个 POS 系统,但我无法为每个文件声明调用方法以访问所述方法

main.cpp主.cpp

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

    UI.MainMenu();
}

AppUI.cpp应用程序接口.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 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应用程序接口.h

#ifndef APPUI_H
#define APPUI_H


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

    private:
};

#endif // APPUI_H

EditBook.cpp编辑本.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编辑本.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安全系统.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安全系统h

#ifndef SECURITYSYS_H
#define SECURITYSYS_H


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

    private:

};

#endif

When I add AppUI UI;当我添加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.AppUI.cpp EditBook.cppSecuritySys.cpp的顶部我收到一条错误消息,指出 UI、SecSysFunc 和 BookFunc 的多重定义。 But if I don't add them at the top, then the BookFunc.AddBook();但是如果我不在顶部添加它们,那么BookFunc.AddBook(); for example, the method can't be recognized and therefor can't be called or used.例如,无法识别该方法,因此无法调用或使用。 What am I doing wrong here?我在这里做错了什么?

You are getting a multiple definition error because you are just redeclaring the objects UI , SecSysFunc and BookFunc .您收到多重定义错误,因为您只是重新声明对象UISecSysFuncBookFunc 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.您需要向编译器明确指出这些标识符指向相同的 object。您可以通过选择四个声明之一作为定义来实现,将这些行保持原样。 But to the declarations in other code files add extern before the declaration.但是对于其他代码文件中的声明,在声明之前添加extern This will tell the compiler to look in other linked object files for the definition of those objects这将告诉编译器在其他链接的 object 文件中查找这些对象的定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从不同的cpp访问类函数(如何声明) - Access class function from different cpp (how to declare) 如何在C ++中的不同文件中声明类? - How to declare class in different files in c++? 如何从在其中创建的不同类对象中访问类对象的成员函数? - How do you access member functions of a class object from within a different class object that has been created in it? 在Qt中声明const并从2个不同的类进行访问 - Declare const in Qt and access from 2 different classes 如何使用不同的线程访问Singleton类成员函数? - How to access Singleton class member functions using different threads? 当我使用多态性和 inheritance 将其声明为指向具体 class 的指针时,如何访问具体 class 的成员函数 - How do I access member functions of concrete class when I'm using Polymorphism and inheritance to declare it as a pointer to the concrete class 如何从 2 个不同的文件访问结构数组? - How to access a structure array from 2 different files? C ++-如何模拟来自不同类,不同标头的函数? - C++ - how to mock functions from different class, different headers? 如何正确地从另一个类访问类函数? - How to properly access class functions from another class? 如何在类外声明专业化模板方法? - How to declare specialization template method outside class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM