[odb-users] MS SQL Foreign Key Error
Boris Kolpackov
boris at codesynthesis.com
Mon Apr 1 07:30:55 EDT 2013
Hi Dean,
Dean Throop <DThroop at pacificcabinets.com> writes:
> I have a Products table with a to-one relationship to an ItemTypes table.
Can you show the definitions for these tables (i.e., the CREATE TABLE
statements).
> for(std::vector<ItemType>::const_iterator i(productTypes.begin ());i!=productTypes.end ();++i)
> {
> if(i->GetName()==currentProductType)
> {
> ItemType theValue=*i;
> productType=&theValue;
> break;
> }
> }
This code is not going to work. You are creating a local copy (on the
stack) of the ItemType object (theValue) and then assign a pointer to
it to the data member (productType). You are lucky you are getting an
exception. Normally you would end up with access violation.
A quick and dirty fix would be to instead assign a pointer to the object
that is in the vector (you will either need to pass non-const reference
to the vector of make productType const):
productType=&*i;
Better yet, use std::shared_ptr to manage your objects.
Boris
More information about the odb-users
mailing list