使用类作为向量类型时出错 - "Undeclared identifier"

ERROR using a class as a vector type - "Undeclared identifier"

本文关键字:出错 Undeclared identifier 类型 向量      更新时间:2023-10-16

你好堆栈溢出!

我遇到了一个我试图解决但没有成功的问题,因此我转向您希望得到答案的程序员。

我怀疑这个问题的是,它与我的 main.cpp 有关,由于原因,我尚未开始编码。可能是非常错误的。

任何提示/提示都会受到赞赏!

错误是在库存标题中的该部分

// Vector
   static std::vector<accounts> users;

错误消息在底部列出。

我包含了两个类标题文件。


main.cpp:

// Classes
#include "BankFunctions.h" // Handles the bank functions
#include "accounts.h" // Handles the customer accounts
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
int main() {
    return 0;
}

类:帐户

标题:

#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "BankFunctions.h"
class accounts
{
    public:
        // Constructor
        accounts(
            unsigned int newId, 
            unsigned int newAge, 
            unsigned int newSSN, 
            std::string newFirstName,
            std::string newLastName, 
            std::string newAdress, 
            std::string newEmail, 
            double newBalance
        );
        // Overload constructor
        accounts(std::string eId, std::string eNAge, std::string eSSN, std::string eFName, std::string eLName, std::string eEmail, std::string eAdress, std::string eNBalance);
        // Mutators
        inline void setId(unsigned int i) { id = i; }
        inline void setAge(unsigned int a) { age = a; }
        inline void setSSN(unsigned int ssn) { SSN = ssn; }
        inline void setFirstName(std::string FN) { firstName = FN; }
        inline void setLastName(std::string LN) { lastName = LN; }
        inline void setEmail(std::string em) { email = em; }
        inline void setAdress(std::string adr) { adress = adr; }
        inline void setBalance(double newBalance, bool t);
        // Accessors
        inline unsigned int getId() const { return id; }
        inline unsigned int getAge() const { return age; }
        inline unsigned int getSSN() const { return SSN; }
        inline std::string getFirstName() const { return firstName; }
        inline std::string getLastName() const { return lastName; }
        inline std::string getEmail() const { return email; }
        inline std::string getAdress() const { return adress; }
        inline double getBalance() const { return balance; }
    private:
        // Customer account details
        unsigned int id;
        unsigned int age;
        unsigned int SSN; // Social Security Number
        std::string firstName;
        std::string lastName;
        std::string adress;
        std::string email;
        double balance;
};

类2:BankFunctions

标题:

#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "accounts.h"
class BankFunctions
{
    public:
        BankFunctions();
        static void loadVector(); // Loads in customer account information into objects that get stored into a vector. (All the existing accounts get loaded in)
        static void newCustomer(); // Create new customer account
        static void existingCustomer(); // View customer account
        static void deposit(unsigned int accId); // Deposit money function
        static void withdraw(unsigned int accId); // Withdraw money function
        // Edit customer account
        static void editCustomerDetails(unsigned int accId);
            // Related Functions
            static void editAge(unsigned int accId);
            static void editSSN(unsigned int accId);
            static void editFirstName(unsigned int accId);
            static void editLastName(unsigned int accId);
            static void editAdress(unsigned int accId);
            static void editEmail(unsigned int accId);
            static void editBalance(unsigned int accId);
    private:
        // Vector
        static std::vector<accounts> users;
        static unsigned int amountOfAccounts;
};

错误消息:

...bankfunctions.h(36): error C2065: 'accounts': undeclared identifier
...bankfunctions.h(36): error C2923: 'std::vector': 'accounts' is not a valid template type argument for parameter '_Ty'
...bankfunctions.h(36): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type

尝试从帐户中删除 include BankFunctions.h

我认为,问题是,在其中包含完成的位置的整个标题文件中,请简单地插入整个标头文件,但是pragma once确保您不会将标题不包括在CPP文件中。当您的标题彼此包含时,可以将accounts的类定义超过bankfunctions的第36行。

您也可以在main.cpp的最顶部放置一个额外的class accounts;。这也应该起作用,但不是一个漂亮的解决方案。