1

我正在尝试使用 CodeBlocks 上名为 LibXL [for C++] 的第三方库为我的 Mini 项目执行此代码。

#include "libxl.h"
#include <iostream>
#include<string>
#include<tchar.h>

#ifdef _UNICODE
typedef WCHAR TCHAR;
#else
typedef char TCHAR;
#endif


using namespace libxl;
using namespace std;
    //Ignore this block of comments //Specifically for CinCout 
/*
class Student_Information
{
    string First_Name, Last_Name,Mobile, Course;
    double bd_day, bd_month, bd_year,Gr_No;

   // Format* format1 = info_book->addFormat();


public:

Student_Information()
{

Book* info_book = xlCreateBook();
Sheet* sheet1 = info_book->addSheet("Sheet1");


}
    void insert_info()
    {
        int num;
        info_book->load("student_information.xls");
}*/

class Student_Information
{
    string First_Name, Last_Name,Mobile, Course;
    int bd_day, bd_month, bd_year,Gr_No;
     Book* info_book = xlCreateBook();  //Throws are warning "non-static data member initializers only available with -std=c++11 or -std=gnu++11"
    Sheet* sheet1 = info_book->addSheet("Sheet1"); //Throws the same warning even over here


public:

    void insert_info()
    {
        int num;
        info_book->load("student_information.xls");

        do
        {
            cout<<"Enter 1 to add a record | Enter 0 to exit";
            cin>>num;

            cout<<"Please enter the GR Number of the Student: ";
            cin>>Gr_No;
            sheet1->writeNum(sheet1->lastRow(),0,Gr_No);

            cout<<"Please Enter your FIRST NAME [In Capitals]: ";
            cin>>First_Name;
            sheet1->writeStr(sheet1->lastRow(),1,First_Name.c_str());

            cout<<"Please Enter your LAST NAME [In Capitals]: ";
            cin>>Last_Name;

            sheet1->writeStr(sheet1->lastRow(),2,Last_Name.c_str());

            sheet1->writeStr(sheet1->lastRow(),3,"BCA");

            cout<<"Please enter your 10 Digit mobile number: ";
            cin>>Mobile;

            sheet1->writeStr(sheet1->lastRow(),4,Mobile.c_str());

            info_book->save("student_information.xls");
        }
        while(num!=0);
       // info_book->load("student_information.xls");


       // info_book->release();

    }
};

int main()
{
    Student_Information ob1;
    ob1.insert_info();
}

当我尝试编译代码时,我收到了我在代码中提到的两个警告,但编译器没有显示错误

执行程序时,程序要求提供 2 个选项。当用户选择选项 1 时,程序会询问 GR 编号。输入 GR 编号后,它会在屏幕上显示此消息时崩溃:

在此处输入图像描述

我对这个错误感到很困惑。我尝试清理项目并重建它,但没有帮助。我正在使用带有编码器 UTF-8 和 GNU GCC 编译器的代码块。

如果我不恰当地提及事情而冒犯了 21 世纪的极客,我深表歉意。我对 LibXL 很不熟悉,而且对一般的编码也不太熟悉。

4

1 回答 1

0

书本通过load()函数加载后,其旧内容被删除并被从文件中加载的新内容替换,sheet1将指向无效的内容并导致程序崩溃。

一个快速的解决方案是在每次加载书籍时找到新的 Sheet1 值:

Sheet* getSheetByName(Book* book, const TCHAR * name)
{
   for(unsigned short i = 0; i < book->sheetCount(); ++i)
   {
       if(_tcscmp(book->getSheet(i)->name(), name) == 0)
       {
           return book->getSheet(i);
       }
   }
   return NULL;
}

void insert_info()
{
    int num;
    info_book->load("student_information.xls");
    sheet1 = getSheetByName(info_book, "Sheet1");

    if( sheet1 == NULL) { //"Sheet1" not found, add a new one.
        sheet1 = info_book->addSheet("Sheet1");
    }

    do
    {
        //insert data to sheet1 ....
    }

}
于 2018-12-12T12:29:08.330 回答