gpt4 book ai didi

c++ - 使用 LibXL 时执行 C++ (CodeBlocks) 代码时出错

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:06 28 4
gpt4 key购买 nike

我正在尝试在 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 编号后,它崩溃并在屏幕上显示此消息:

enter image description here

我对这个错误很困惑。我尝试清理项目并重建它但没有帮助。我将 Codeblocks 与编码器 UTF-8 和 GNU GCC 编译器一起使用。

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

最佳答案

书被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 ....
}

}

关于c++ - 使用 LibXL 时执行 C++ (CodeBlocks) 代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53736762/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com