如何返回PQXX连接并保存到其他变量
how to return pqxx connection and save into other variable?
// DataBaseConn.cpp
#include <iostream>
#include "yaml-cpp/yaml.h"
#include "DatabaseConn.h"
connection DatabaseConn::getConn() {
YAML::Node config = YAML::LoadFile("database.yaml");
std::string psql_user = config["production"]["user"].as<string>();
std::string psql_pass = config["production"]["password"].as<string>();
connection C("dbname=demo user="+ psql_user +" password="+ psql_pass +" hostaddr=127.0.0.1 port=5432");
if (C.is_open()) {
std::cout << "Opened database successfully: " << C.dbname() << std::endl;
} else {
std::cout << "Can't open database" << std::endl;
throw "Database Connection Error";
}
return pqxx::basic_connection<connect_direct>(C);
}
并试图将连接保存在其他变量中以供以后使用
connection conn = DatabaseConn().getConn();
这可能吗?我做错了什么?我是c++新手。
编译错误:
/usr/local/include/pqxx/basic_connection.hxx:54:40: error: within this context
template<typename CONNECTPOLICY> class basic_connection :
^
main.cpp: In function ‘int main()’:
main.cpp:20:50: note: synthesized method ‘pqxx::basic_connection<pqxx::connect_direct>::basic_connection(const pqxx::basic_connection<pqxx::connect_direct>&)’ first required here
connection conn = DatabaseConn().getConn();
连接对象可能不可复制也不可移动,因此不能按值返回。考虑通过new操作符在堆上分配它,并返回指向它的指针:
connection *DatabaseConn::getConn()
{
// ...omitted...
return new pqxx::basic_connection<connect_direct>(C);
}
调用方负责删除返回的连接。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 如何在-120到120之间更改int到char(或其他简单保存为一个字节的简单方法)
- 在参数包中获取函数指针的返回类型,并将其保存为与其他参数连接的元组
- 通过其他线程通过JNI呼叫保存的Java对象
- 在POSIX中,我可以保存信号以供调用条件等待的其他线程使用吗.(这些线程来自同一进程)
- C++不会通过其他方法保存更改
- 将二进制文件内容保存在字符串中,以便能够对其进行 md5 并在以后与其他文件进行比较
- 将非成员变量数据传递给构造函数时,如何保存它们并在其他成员函数中使用?C++
- 指针是否只保存指向其他变量的地址
- c++应用程序保存并应用首选项模式(可能是boost或其他什么)
- 我需要帮助在其他地方保存我的LOG.TXT文件
- 如何创建一个类来保存另一个类的对象数组,如何操作其他类的私有属性
- 使用fstream将其他文件保存在自己的数据存储中
- 如何返回PQXX连接并保存到其他变量
- 如何在c++中读取以空格分隔的数据并将其保存到数组中,然后以其他顺序写入新的文本文件
- 在使用mkdir()创建一个文件夹后,我如何让程序将文件和其他信息保存到新文件夹中?