0

I'm trying to create a workaround using implied pointers to obscure most of the internals of a library whilst still retaining basic functionality. Currently I'm relying on templates and the code below produces the following compiler error that I don't know how to solve:

prog.cpp: In function ‘int main()’:
prog.cpp:55:10: error: no match for ‘operator=’ (operand types are ‘std::auto_ptr<TypeAdapter_impl<mytype> >’ and ‘TypeAdapter_impl<mytype>*’)  t._impl = tam;

Here is the code:

#include <iostream>
#include <memory>
using namespace std;

typedef long document_ptr;

class mytype {
    public:
    mytype(document_ptr d) {
        a = d;
        std::cout << "Instantiated mytype with document_ptr " << a << "\n";
    }
    document_ptr a;
};

class TypeContainer {
public:
    void * _t; 
    TypeContainer(void * t) {  _t = t; }
    ~TypeContainer() { delete _t; }
    // TODO operator *
};

class DocumentContainer {
    public:
    document_ptr * doc;
};

template<class Type>
class TypeAdapter_impl
{
    public:
    TypeAdapter_impl() { }
    ~TypeAdapter_impl() { }

    TypeContainer TypeFactory(DocumentContainer& d){
        Type * t = new Type(d.doc);
        return TypeContainer(t);
    }
};

template<class Type>
class TypeAdapter
{
    public:
    std::auto_ptr< TypeAdapter_impl<Type> > _impl;
};



int main() {
    // your code goes here
    TypeAdapter<mytype> t;
    TypeAdapter_impl<mytype> * tam = new TypeAdapter_impl<mytype>;
    t._impl = tam;
    DocumentContainer d;
    d.doc = new document_ptr(10);
    mytype m = t._impl->TypeFactory(d);
    return 0;
}

Any help would be much appreciated!

1 Answer 1

1

The error you mentioned in your question is caused by:

  1. There is no operator= in std::auto_ptr where the RHS is of type T*.
  2. The constructor std::auto_ptr::auto_ptr(T*) is explicit (See reference).

This does not work.

std::auto_ptr<int> a;
int* b = new int;
a = b;

This works.

std::auto_ptr<int> a;
int* b = new int;
a = std::auto_ptr<int>(b);

For your case, change the line

t._impl = tam;

to

t._impl = std::auto_ptr<TypeAdapter_impl<mytype>>(tam);

to remove the compiler error.

There are other problems in your code.

~TypeContainer() { delete _t; }

is not going to work since type of _t is void*.

mytype m = t._impl->TypeFactory(d);

is not going to work since the return type of TypeAdapter_impl::TypeFactory() is TypeContainer and there is no way to convert a TypeContainer to mytype.

The line

    Type * t = new Type(d.doc); // FIXME document_ptr not defined here!

also is not right. doc, as defined in main points to an array of 10 elements. Not sure which one you are interested in using here. Changing it to:

    Type * t = new Type(d.doc[0]);

removes the compiler error.

Sign up to request clarification or add additional context in comments.

1 Comment

I've removed the comments from my code but did not update it to reflect your corrections. The last line you mentioned should indeed dereference the pointer d.doc but it is not an array, it simply a long int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.