#include <iostream>
#include <libxl.h>
#include <cstdlib>
#include <memory>
#include <string>
#pragma comment(lib,"libxl.lib")
const std::string filepath = "H:\\szubooktest.xlsx";
int main()
{
std::shared_ptr<libxl::Book> szu_book = std::make_shared<libxl::Book>(xlCreateXMLBookA());
if (szu_book->load(filepath.data())) std::cout << "文档打开成功!" << std::endl;
else
{
std::cout << "文档打开失败!" << std::endl;
system("pause");
return 0;
}
system("pause");
return 0;
}
如果不写智能指针就没问题
#include <iostream>
#include <libxl.h>
#include <cstdlib>
#include <memory>
#include <string>
#pragma comment(lib,"libxl.lib")
const std::string filepath = "H:\\szubooktest.xlsx";
int main()
{
//std::shared_ptr<libxl::Book> szu_book = std::make_shared<libxl::Book>(xlCreateXMLBook());
libxl::Book* szu_book = xlCreateBook();
if (szu_book->load(filepath.data())) std::cout << "文档打开成功!" << std::endl;
else
{
std::cout << "文档打开失败!" << std::endl;
system("pause");
return 0;
}
system("pause");
return 0;
}
模板的模板 对于 make_shared 有点复杂 需要用到 initializer_list
你可以看看这个
http://t.csdn.cn/FXcyr
直接试试
std::shared_ptr<libxl::Book> szu_book(xlCreateXMLBook());
不知道你这个问题是否已经解决, 如果还没有解决的话:创建 book 包,然后创建 Book 类。包括:名字、作者、价格、种类、是否被借出。在类里面写好构造方法,重写 String 方法:
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true)?" 已借出 ":" 未借出 ") +
'}';
}
}
在C++中使用智能指针时,会出现模板无法实例化的错误,可能的原因是第三方库中的Book对象是一个模板类,并不是一个具体的类型。智能指针在使用时需要知道指针所指向的对象的具体类型,以便正确地管理内存。然而,由于模板类在编译时并不会生成实际的类定义,而是在使用时根据模板参数生成对应的类定义,所以在编译阶段无法确定智能指针所指向的具体类型,从而导致模板无法实例化的错误。
解决这个问题的方法有两种:
```cpp #include #include "ThirdPartyLibrary.h" // 包含第三方库的头文件 using namespace std;
// 使用unique_ptr指向具体类型Book unique_ptr> bookPtr(new Book(args));
// 或者使用shared_ptr shared_ptr> bookPtr = make_shared>(args); ```
```cpp #include #include "ThirdPartyLibrary.h" // 包含第三方库的头文件 using namespace std;
void* bookRawPtr = new Book(args); // 创建void指针来指向Book对象的指针
// 使用智能指针进行管理时,需要将void指针转换为原始类型 unique_ptr> bookPtr(reinterpret_cast*>(bookRawPtr));
// 使用智能指针进行操作时,需要将智能指针转换为void指针后再传递给第三方库 ThirdPartyLibraryFunction(reinterpret_cast(bookPtr.get())); ```
需要注意的是,在使用智能指针管理Book对象的内存时,如果第三方库需要原始指针,需要将智能指针转换为原始指针后再传递给第三方库。在析构智能指针时,会自动释放所管理的内存,无需手动删除。