c++ - 错误: aggregate has incomplete type and cannot be defined

标签 c++ c++11 incomplete-type

我正在为银行编写一个程序,当我编译它时遇到此错误。 这是我的整个代码,因为我无法在这里分享更多代码,它位于粘贴箱上。 My code

我没有包括我的整个代码,但我认为负责的部分:

#include<iostream>
#include<string>
#include<limits>
#include<fstream>
#include<iomanip>

using std::ios_base;
using std::ostringstream;
using std::ios;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::stoi;
using std::to_string;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;

class yourbankaccount
{
private:
    string accountNumber;
    // Name of the customer.
    string name;
    // Date of birth
    int day;
    int month;
    int year;
    // Get Age
    int age;
    // Get Gender
    string gender;
    long accountBalance = 0;
public:
    friend int getInt(string info, int length);
    friend string getStr(string info, int length);
    yourbankaccount(){}
    void setAccountNumber(string number){
        accountNumber = number;
    }
    string shareAccountNumber(){
        return accountNumber;
    }
    // Get date
    void getDate(){
        int d, m, y;
        cout << "Now please enter your Date Of Birth(DD/MM/YYYY):"<<endl;
        d = getInt("Day(DD)", 2);
        m = getInt("Month(MM)", 2);
        y = getInt("Year(YYYY)", 4);
        day = d; month = m; year = y;
    }
    // Return Date
    string sendDate(){
        string Date = to_string(day) + "/" + to_string(month) + "/" + to_string(year);
        return Date;
    }
    // Method to add details.
    void getCustomerDetails(){
        bool gotGen = false;
        string n; int a; unsigned int p; char g;
        cout << "Please fill in the following details: ";
        name = getStr("Name of Account holder", 45);
        cout << "Enter your birthday in given format: ";
        getDate();
        age = getInt("your Age", 2);
        do
        {
            gender = getStr("your Gender(only M for Male, F for Female, O for Other)", 1);
            if (stringToUpper(gender)  != "M" || 
                stringToUpper(gender) != "F" || 
                stringToUpper(gender) != "O"){
                    "Please enter only M or F or O!";
            }else{
                gotGen = true;
            }
        } while (gotGen);
    } 
    string shareName(){
        return name;
    }
    string shareDOB(){
        return sendDate();
    }
    int shareAge(){
        return age;
    }
    string shareGender(){
        return gender;
    }
    void setAccountBalance(long bal){
        accountBalance = bal;
    }
    long shareAccountBalance(){
        return accountBalance;
    }
    ~yourbankaccount(){}
};

我在这里声明该对象:

void createAccount(){
    ofstream addAccount;
    yourbankaccount Account;
    cout << "Thank You for Choosing Your Bank.";
    cout << "Please provide The folling details.";
    Account.setAccountNumber(calNewAccNumber());
    Account.getCustomerDetails();
    Account.setAccountBalance(500);
    int pin = getInt("a pin for the account", 4);
    string file ="accounts/open.csv";
    addAccount.open(file, ios::out | ios::app);
    if (addAccount.is_open())
    {
        addAccount << Account.shareAccountNumber()
        << "," << pin << "," << "\"" << Account.shareName() << "\"" << "," 
        << Account.shareDOB() <<"," << Account.shareAge()
        << "," << Account.shareGender() << Account.shareAccountBalance()<<"\n";
    }

    cout << "Your Account has been created Sucessfully." <<
            "Please note your number as it will be required for logging in\n";
    cout << "Account number is:" << Account.shareAccountNumber() << "\n";
}

编译时出现此错误:

> Executing task: C/C++: g++.exe build active file ver(1) <

Starting build...
D:\Programs\mingw_w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe -g D:\BSC_IT\sem_2\OOPS\CA2\YourBank\yourbank.cpp -o 
D:\BSC_IT\sem_2\OOPS\CA2\YourBank\yourbank.exe
```
D:\BSC_IT\sem_2\OOPS\CA2\YourBank\yourbank.cpp: In function 'void createAccount()':
D:\BSC_IT\sem_2\OOPS\CA2\YourBank\yourbank.cpp:139:21: error: aggregate 'yourbankaccount Account' has incomplete type and cannot be defined
    yourbankaccount Account;
                     ^~~~~~~
```
Build finished with error(s).
The terminal process terminated with exit code: -1.

>Terminal will be reused by tasks, press any key to close it.

最佳答案

您的类(class)类型是incomplete 。您需要define您的类您可以声明该类的对象。 forward declare 还不够类(class)。

关于c++ - 错误: aggregate has incomplete type and cannot be defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66841550/

相关文章:

c++ - 在库 : symbol structure query 上使用 Breakpad

c++ - 使用 stringstream 从 TCP 套接字读取

c++ - 未调用 move 构造函数

c - 使用 typedef 和结构时出错

c++ - 标准容器模板可以用不完整的类型实例化吗?

c++ - 排序时更改排序顺序是未定义的行为吗?

c++ - 将派生类推送到c++中基类的 vector

c++ - 如何控制何时使用 initializer_list。局部变量、返回和传递参数

c - typedef 结构导致 "pointer to incomplete type not allowed"错误

c++ "no appropriate default constructor available"错误使用模板类数据成员