Instantation of odb::result<my_type> e.g. odb::query<my_type> fails - odb

I'm writing my first odb code and can't get this basic working, although only the db connection code works:
/*! #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
driver code:
// 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;
}
Of course I odb-compiled the overview_record.h with odb.exe --database mssql overview_record.h (otherwise there won't be an .hxx). But I get the following compiler errors by the line db->query<overview_record>();, although instantiating a default constructed result works:
Error 3 error C2504: 'odb::result_base' : base class undefined c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 76 1 odb_playground
Error 4 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 5 error C2146: syntax error : missing ';' before identifier 'value_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 7 error C2602: 'odb::result::value_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 8 error C2868: 'odb::result::value_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 9 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 10 error C2146: syntax error : missing ';' before identifier 'result_impl_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 12 error C2602: 'odb::result::result_impl_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 13 error C2868: 'odb::result::result_impl_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

Problem was a missing flag of the odb compiler, in this case the -q e.g. --generate-query flag. Otherwise odb won't add the needed code to the generated files.
So the correct invocation would be odb.exe -q --database mssql overview_record.h

Related

Linker error in boost serialization of custom archive

I've tried to implement an own archive type for boost serialization following official boost example to write archives.
#include <iostream>
#include <vector>
#include <boost/serialization/nvp.hpp>
#include "boost/serialization/vector.hpp"
#include <boost/archive/detail/common_oarchive.hpp>
#include <boost/archive/detail/register_archive.hpp>
#include <boost/archive/detail/archive_serializer_map.hpp>
class complete_oarchive : public boost::archive::detail::common_oarchive<complete_oarchive>
{
friend class boost::archive::save_access;
template<class T>
void save(T & t){
std::cout << "saved data\n";
}
public:
void save_binary(void *address, std::size_t count){
}
};
template class boost::archive::detail::archive_serializer_map<complete_oarchive>;
template class boost::archive::detail::common_oarchive<complete_oarchive>;
BOOST_SERIALIZATION_REGISTER_ARCHIVE(complete_oarchive)
int main(int argc, char *argv[])
{
std::vector<double> testVector = {1, 2, 3, 4};
complete_oarchive oa;
std::vector<double>* pVec = &testVector;
oa << BOOST_SERIALIZATION_NVP(testVector);
oa << BOOST_SERIALIZATION_NVP(pVec);
return 0;
}
Compiling this example with
g++ -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/demo.o.d" -o build/Debug/GNU-Linux/demo.o demo.cpp
g++ -o dist/Debug/GNU-Linux/serializationdemo build/Debug/GNU-Linux/demo.o -lboost_serialization
leads to the following linker error
build/Debug/GNU-Linux/demo.o: In function `boost::archive::detail::pointer_oserializer<complete_oarchive, std::vector<double, std::allocator<double> > >::pointer_oserializer()':
/opt/tools/boost/boostRdk-1.66.0/include/boost/archive/detail/oserializer.hpp:222: undefined reference to `boost::archive::detail::archive_serializer_map<complete_oarchive>::insert(boost::archive::detail::basic_serializer const*)'
build/Debug/GNU-Linux/demo.o: In function `boost::archive::detail::pointer_oserializer<complete_oarchive, std::vector<double, std::allocator<double> > >::~pointer_oserializer()':
/opt/tools/boost/boostRdk-1.66.0/include/boost/archive/detail/oserializer.hpp:227: undefined reference to `boost::archive::detail::archive_serializer_map<complete_oarchive>::erase(boost::archive::detail::basic_serializer const*)'
collect2: error: ld returned 1 exit status
It seems that serializing a pointer in
oa << BOOST_SERIALIZATION_NVP(pVec);
leads to this error. After deletion of this line everything works fine and the result is as expected.
Does anybody have experience in writting own serialization archives?
A simimal Problem was solved here
https://groups.google.com/forum/#!topic/boost-list/CMoDosGZUo8
but I wasn't able to solve this by forward declarations.
I solved the issue by replacing
#include <boost/archive/detail/archive_serializer_map.hpp>
by
#include <boost/archive/impl/archive_serializer_map.ipp>

log4cplus- helpers::towstring() and PropertyConfigurator::doConfigure scramble string parameter in C++/CLI running in non-CLR mode for x64

I'm using log4cplus as a logger for both CLR and non-CLR C++/CLI code and C# code so for that reason I'm using the Unicode x64 build of log4cplus, log4cplusU.lib/dll.
If I run the following code in a non-CLR C++/CLI x64 console application, I get a memory access exception.
int _tmain(int argc, _TCHAR* argv[])
{
std::string LogFileName = "log4cplus.log";
auto db = log4cplus::helpers::towstring(LogFileName);
Exception:
Unhandled exception at 0x00007FF8E4A1CDA1 (msvcr120.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
What's up?
I'm using Visual Studio 2013. My call stack at the exception looks like:
> log4cplusU.dll!std::vector<wchar_t,std::allocator<wchar_t> >::vector<wchar_t,std::allocator<wchar_t> >(unsigned __int64 _Count) Line 691 C++
log4cplusU.dll!log4cplus::helpers::towstring_internal(std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > & outstr, const char * src, unsigned __int64 size, const std::locale & loc) Line 70 C++
log4cplusU.dll!log4cplus::helpers::towstring(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & src) Line 124 C++
ConsoleApplication1.exe!wmain(int argc, wchar_t * * argv) Line 24 C++
ConsoleApplication1.exe!__tmainCRTStartup() Line 623 C
At the point where the exception fires in std::vector(size_type), _Count is a crazy number.
_Count 14757396612626683276 unsigned __int64
The reason appears to be that the string parameter gets scrambled or misinterpreted.
The same problem manifests itself on non-Unicode DEBUG MODE builds of log4cplus in unmanaged code on VS but not in release mode builds.
For example:
#include <string>
#include "stdafx.h"
#include <iostream>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
int _tmain(int argc, _TCHAR* argv[])
{
std::string LogConfigFileName = "WhenLoggingCppManagedCode.properties";
try
{
log4cplus::tstring cfn = LogConfigFileName;
log4cplus::PropertyConfigurator::doConfigure(cfn);
std::cout << "Good Deadpool." << std::endl;
}
catch (...)
{
std::cout << "BAD Deadpool." << std::endl;
}
std::cin.get();
return 0;
}

ANTLR3 c target: when rule fail, action still execute, i can't understand

when rule fail, action still execute, why?
There is my test:
grammar Test;
options{
language = C;
}
#parser::includes
{
#include <iostream>
}
#lexer::includes
{
#include <iostream>
}
rScript
: INT LR RR{std::cout << "Action Run...\n";};
INT :
('0'..'9')+;
LR : '{';
RR : '}';
main fun:
int main()
{
char teststr1[] = "111dd}";
pANTLR3_INPUT_STREAM input;
input = antlr3StringStreamNew((pANTLR3_UINT8)teststr1, ANTLR3_ENC_8BIT, sizeof(teststr1) - 1, (pANTLR3_UINT8)"-memory-");
pTestLexer Lex = TestLexerNew(input);
pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(
ANTLR3_SIZE_HINT, TOKENSOURCE(Lex));
pTestParser psr = TestParserNew(tstream);
psr->rScript(psr);
int ParserErrorCount = psr->pParser->rec->state->errorCount;
int LexerErrorCount = Lex->pLexer->rec->state->errorCount;
std::cout << "Parser Error Count: " << ParserErrorCount << "\n";
std::cout << "Lexer Error Count: " << LexerErrorCount << "\n";
getchar();
return 0;
}
Output:
-memory-(1) : lexer error 3 :
at offset 4, near 'd' :
dd}
-memory-(1) : lexer error 3 :
at offset 5, near 'd' :
d}
-memory-(1) : error 10 : org.antlr.runtime.MissingTokenException, at offset 5
near [Index: 0 (Start: 0-Stop: 0) ='<missing LR>', type<5> Line: 1 LinePos:5]
: Missing LR
Action Run...
Parser Error Count: 1
Lexer Error Count: 2
My question has two:
I can't understand, why the rule match fail, but the action still run?
How to exit parser when first error catch(lexer or parser error), i have no idea for this, please help.
thanks in advance.

wcout function does not print a french character

I am using the wcin in order to store a single character in a wchar_t. Then I try to print it with a wcout call and the french character 'é' : but I can't see it at my console.
My compiler is g++ 4.5.4 and my OS is Ubuntu 12.10 64 bits.
Here is my attempt (wideChars.cpp) :
#include <iostream>
int main(){
using namespace std;
wchar_t aChar;
cout << "Enter your char : ";
wcin >> aChar;
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
When I lauch the programm :
$ ./wideChars
Enter your char : é
You entered .
So, what's wrong with this code ?
First, add some error checking. Test what does wcin.good() return after the input and what does wcout.good() return after the "You entered" print? I suspect one of those will return false.
What are your LANG and LC_* environment variables set to?
Then try to fix this by adding this at the top of your main(): wcin.imbue(std::locale("")); wcout.imbue(std::locale(""));
I do not have my Ubuntu at hand right now, so I am flying blind here and mostly guessing.
UPDATE
If the above suggestion does not help then try to construct locale like this and imbue() this locale instead.
std::locale loc (
std::locale (),
new std::codecvt_byname<wchar_t, char, std::mbstate_t>("")));
UPDATE 2
Here is what works for me. The key is to set the C locale as well. IMHO, this is a bug in GNU C++ standard library implementation. Unless I am mistaken, setting std::locale::global(""); should also set the C library locale.
#include <iostream>
#include <locale>
#include <clocale>
#define DUMP(x) do { std::wcerr << #x ": " << x << "\n"; } while (0)
int main(){
using namespace std;
std::locale loc ("");
std::locale::global (loc);
DUMP(std::setlocale(LC_ALL, NULL));
DUMP(std::setlocale(LC_ALL, ""));
wcin.imbue (loc);
DUMP (wcin.good());
wchar_t aChar = 0;
wcin >> aChar;
DUMP (wcin.good());
DUMP ((int)aChar);
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
UPDATE 3
I am confused, now I cannot reproduce it again and setting std::locale::global(loc); seems to do the right thing wrt/ the C locale as well.

C++ CLI two stack<>

problems in use two cliext::stack..
stack<T^>^ pfn = gcnew stack<T^>(); - compiles
stack<K^>^ pf = gcnew stack<K^>(); - 93 compile errors.
What is the problem?
USE Generics::Stack Why is the declaration of the two machines a lot of mistakes?
private: System::Void btn_add_Click(System::Object^ sender, System::EventArgs^ e)
{
fileOperation^ fw = gcnew fileOperation();
if (comboBox1->SelectedIndex == 0 )
{
flyBird^ f = gcnew flyBird();
System::Collections::Generic::Stack<flyBird^>^ p = gcnew System::Collections::Generic::Stack<flyBird^>();
f->birdName=txt_name->Text;
f-> year = textBox1->Text;
f->vess = txt_tmp1->Text;
f->sreda = txt_tmp2->Text;
p->Push(f);
}
else
{
System::Collections::Generic::Stack<noflyBird^>^ p1 = gcnew System::Collections::Generic::Stack<noflyBird^>();
noFlyBird^ f = gcnew noFlyBird();
fn ->birdName=txt_name->Text;
fn ->year = textBox1->Text;
fn ->pitanie = txt_tmp1->Text;
fn ->domoshnie = txt_tmp2->Text;*/
}
}
Warning 1 warning C4138: '*/' found outside of comment c:\documents and settings\bobilev\my documents\visual studio 2010\projects\burd\burd\Form1.h 275 1 burd
Error 6 error C2143: syntax error : missing ';' before '}' \my documents\visual studio 2010\projects\burd\burd\Form1.h 277 1 burd
Error 7 error C2143: syntax error : missing ';' before '}' ments\visual studio 2010\projects\burd\burd\Form1.h 281 1 burd
Error 11 error C2143: syntax error : missing ';' before '}' my documents\visual studio 2010\projects\burd\burd\Form1.h 293 1 burd
Error 12 error C2143: syntax error : missing ';' before '}' c:\my documents\visual studio 2010\projects\burd\burd\Form1.h 297 1 burd
Looking at the compile errors you have, the first is an extra */ end comment without a /* to begin the comment. We can see that in the code you posted, at the end of the else block. That error is on line 275 of your code. The other compile errors you posted are on line 277 and later, so it looks like the problem is AFTER the code you posted here.