使用类作为矢量类型的错误 - "未声明的标识符"

时间:2017-12-08 20:03:39

标签: c++

Hello Stack溢出!

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

我怀疑这个问题是,它与我的 main.cpp 有关,但我还没有开始编码。可能是非常错误的。

任何提示/提示都表示赞赏!

错误是关于此部分位于BankFunctions标题

// 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;
}

第1类:帐户

部首:

#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

1 个答案:

答案 0 :(得分:1)

尝试从accounts.h中删除include BankFunctions.h

我认为,问题在于include只执行整个头文件插入包含的位置,但是pragma once确保您不在cpp文件中包含两次头文件。 当您的标题彼此包含时,可以通过BankFunctions.h的第36行获取accounts的类定义。

您还可以在main.cpp的顶部添加额外的class accounts;。这也应该有效,但不是一个很好的解决方案。