我试图使用一个名为LibXLfor C++ on CodeBlocks的第三方库为我的迷你项目执行这段代码。
#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();
}当我试图编译代码时,我得到了我在代码中提到的两个警告,但是编译器没有显示错误。
当程序被执行时,程序要求有两个选项。当用户选择选项1时,程序会要求GR编号。输入GR编号后,它会崩溃,在屏幕上显示以下消息:

我对这个错误感到很困惑。我试着清理和重建这个项目,但是没有帮助。我使用代码块与编码器UTF-8和GNU GCC编译器。
如果我不恰当地提到一些事情,冒犯了21世纪的极客们,我很抱歉。我对LibXL相当陌生,而且对一般的编码也不太熟悉。
发布于 2018-12-12 12:29:08
在书籍被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 ....
}
}https://stackoverflow.com/questions/53736762
复制相似问题