Sign in to follow this  
AcidZombie24

WTF adapt ctor

Recommended Posts

-edit- BRB, it works when i have a ctor() in someclass. The original code dude and i accidently deleted it. But the original doesnt work. I'll copy/paste chunks until i get to this problem again. -edit2- I see more replys, sorry guys :X. it was a bad test case. I had multiple In's and didnt realize i forgot the ctor() in one of them. I do still have a problem relating to something else. brb I have no idea why this isnt compiling. It doesnt compile under msvc or gcc the error says i didnt give it the correct constructor. It looks fine to me. test.cpp(1137) : error C2512: 'SomeClass<T>' : no appropriate default constructor available with [ T=int ] test.cpp(1136) : while compiling class template member function 'AdaptMe<Out,In>::AdaptMe(In,int &(__cdecl *)(int &))' with [ Out=Out, In=In ] test.cpp(1160) : see reference to class template instantiation 'AdaptMe<Out,In>' being compiled with [ Out=Out, In=In ]
template <class T>
struct SomeClass
{
private:
	T* ptr;
public:
	typedef T& value_type;
	typedef T& reference;
	 SomeClass(const SomeClass& v)							{ *this = v; }
	 SomeClass(T* pointer_)								{ ptr = pointer_; }
	~SomeClass()									{ }

};


template <class Out, class In>
class AdaptMe : public Out
{
public:
	typedef typename Out::reference (*TranslateFunc)(typename In::reference in);

	In inObj;
	TranslateFunc func;

	//AdaptMe(const In& obj, TranslateFunc Func) : inObj(obj)
	AdaptMe(In obj, TranslateFunc Func) : inObj(obj)
	{
		func = Func;
	}
};

template <class Out, class In>
typename Out::reference func(typename In::reference a)
{
//	static Out::value_type obj = 0;
//	return obj;
	return a;
}
int main()
{
	int ai[] = {0, 5, 3};

	typedef SomeClass<int> Out;
	typedef SomeClass<int> In;

	In i(ai);
	
	const SomeClass<int> &i2 = i;

	AdaptMe<Out, In>adapt(i, func<Out, In>);
	return 0;
}





[Edited by - AcidZombie24 on November 8, 2008 2:18:18 AM]

Share this post


Link to post
Share on other sites
Did you look at the error?

error C2512: 'SomeClass<T>' : no appropriate default constructor available

It gives a good hint.

Comment out this line:

AdaptMe<Out, In>adapt(i, func<Out, In>);


And you'll compile just fine.

Add what your error is complaining about and you'll compile fine even with your offending line.

Share this post


Link to post
Share on other sites
Edit: Too slow =)

The reason for it not compiling is that you don't specify a valid constructor for the base class of AdaptMe, SomeClass<int> in this case. Either add a SomeClass constructor taking no arguments, or explicitly run on one of the existing ones in the AdaptMe constructor.

I.e.
SomeClass() {}

or

AdaptMe(In obj, TranslateFunc Func) : Out(/*something fitting*/), inObj(obj)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this