我正在编写我的第一个odb代码,虽然只有db连接代码才能工作,但无法完成这个基本工作:
/*! @file overview_record.h
*/
#ifndef OVERVIEW_RECORD_H
#define OVERVIEW_RECORD_H
#include <string>
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#pragma db object table("mddb_overview") no_id
class overview_record
{
public:
#pragma db column("product_name") type("nvarchar(64)")
std::wstring product_name;
#pragma db column("order_number") type("int")
long order_number;
};
#endif司机代码:
// odb_playground.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <memory>
#include <thread>
#include <odb/core.hxx>
#include <odb/database.hxx>
#include <odb/mssql/database.hxx>
#include <odb/mssql/connection-factory.hxx>
#include <odb/mssql/exceptions.hxx>
#include "overview_record-odb.hxx"
int main(int argc, char* argv[])
{
try{
std::auto_ptr<odb::mssql::connection_pool_factory> connection_factory(
new odb::mssql::connection_pool_factory(0, std::thread::hardware_concurrency()));
std::unique_ptr<odb::database> db(
new odb::mssql::database("dsn=mddb_local_32", odb::mssql::isolation_read_committed, static_cast<SQLHENV>(0), connection_factory)
);
odb::transaction t(db->begin());
db->query<overview_record>();
//odb::result<overview_record> result();
//auto it = result.begin();
//while(true)
//{
// static int i = 0;
// if (i++ > 10)
// break;
// std::cout << "Order_number " << it->order_number << " product_name " << it->product_name << std::endl;
// ++i;
//}
t.commit();
}
catch (const odb::database_exception &e) {
std::cout << "ODB database error: " << e.what() << std::endl;
}
return 0;
}当然,我用odb.exe --database mssql overview_record.h编译了odb.exe --database mssql overview_record.h(否则不会有.hxx)。但是,我通过行db->query<overview_record>();得到以下编译器错误,尽管实例化默认构造的结果是有效的:
错误3错误C2504:'odb::result_base‘:基类未定义的odb_playground 76 1 odb_playground 错误4错误C2027:未定义类型“odb::result_base”C2027 82 1 odb_playground的使用 错误5错误C2146:语法错误:缺少';‘标识符'value_type’之前的‘value_type’821 odb_playground 错误6错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int C++ 82 1 odb_playground 错误7错误C2602:'odb::result::value_type‘不是’odb::odb::result::value_type‘的基类的成员 错误8错误C2868:'odb::result::value_type‘:使用-声明的非法语法;预期的限定名C2868 82 1 odb_playground 错误9错误C2027:未定义类型'odb::result_base‘odb::result_base 931 odb_playground的使用 错误10错误C2146:语法错误:缺失;标识符'result_impl_type‘之前的’result_impl_type‘931 odb_playground 错误11错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int C++ 93 1 odb_playground 错误12错误C2602:'odb::result::result_impl_type‘不是’odb::odb::result::result_impl_type‘odb_playground基类的成员 错误13错误C2868:'odb::result::result_impl_type‘:使用-声明的非法语法;预期限定名为odb::result::result_impl_type 931 odb_playground
发布于 2015-08-24 14:21:29
问题是缺少了odb编译器的标志,在本例中是-q (例如--generate-query标志)。否则odb将不会将所需的代码添加到生成的文件中。
所以正确的调用应该是odb.exe -q --database mssql overview_record.h
https://stackoverflow.com/questions/32143337
复制相似问题