My native language is C#, so when I started using C++, I wanted to create the get/set sugar syntax for library consumers available in C#.
So I wrote…
template<typename T>
class GetProperty
{
private:
T (*get)();
public:
GetProperty(T (*get)())
{
this->get = get;
}
operator T()
{
return get();
}
template<typename S, typename T>
GetProperty<S> operator=(GetProperty<T> fool)
{
throw 0;
}
};
Then, to to use this, I wrote the code:
template<typename T>
class Vector
{
private:
struct LinkItem
{
public:
T* Item;
LinkItem* Next;
GetProperty<int> Length (&getLength);
LinkItem(T* Item = NULL, int length = 1, LinkItem* Next = NULL)
{
this->Item = Item;
this->length = length;
this->Next = Next;
}
LinkItem& operator =(LinkItem rhs)
{
this->Item = rhs.Item;
this->length = rhs.length;
this->Next = rhs.Next;
return *this;
}
private:
int length;
int getLength()
{
return length;
}
};
LinkItem* current;
.
.
.
};
However, the C/C++ addition on Netbeans (I believe this is the g++ compiler) claims I am instantiating GetProperty with no type.
According to a Google search, this happens if someone forgets a using statement, or to include a header, etc.
But int is a primitive, so this can’t be.
What’s going on?
In addition to the fact that VC++ doesn’t implement non-static data member initializers, you can’t treat the member function getLength as an
int (*)(). Its type isint (LinkItem::*)().You probably shouldn’t be trying to import a foreign idiom like this, but if you really want to you can try something like the following. (Though as Nicol Bolas points out, you also probably shouldn’t be implementing your own linked list, or naming it ‘vector’. If you’re learning C++ just learn the C++ way before going and trying to reinvent things.)