简体   繁体   English

iso c ++禁止无类型的泛型声明

[英]iso c++ forbids declaration of generic with no type

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#. 我的母语是C#,所以当我开始使用C ++时,我想为C#中可用的库使用者创建get / set sugar语法。

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. 但是,Netbeans上的C / C ++添加(我相信这是g ++编译器)声称我实例化了没有类型的GetProperty。
According to a Google search, this happens if someone forgets a using statement, or to include a header, etc. 根据Google搜索,如果有人忘记了using语句或包含标题等,就会发生这种情况。
But int is a primitive, so this can't be. 但是int是原始的,因此不是。
What's going on? 这是怎么回事?

You are constructing GetProperty object while declaring it in a struct. 您正在构造GetProperty对象,同时在结构中声明它。 That is not allowed in C++. 这在C ++中是不允许的。 You have to move the construction to the constructor. 您必须将构造移动到构造函数。

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 (*)() . 除了VC ++不会实现非静态数据成员初始化器这一事实外,您不能将成员函数getLength视为int (*)() Its type is int (LinkItem::*)() . 它的类型是int (LinkItem::*)()

struct Foo {
    int foo(void) {return 1;}
};

int main() {
    int (*var)() = &Foo::foo; // Error
    int (Foo::*var)() = &Foo::foo; // Okay
}

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.) (尽管正如Nicol Bolas指出的那样,您也可能不应该实现自己的链表或将其命名为“向量”。如果您正在学习C ++,则在开始尝试重新发明事物之前,只需学习C ++的方法即可。)

#include <functional>

template<typename T>
class GetProperty
{
private:
    std::function<int()> get;
public:
    GetProperty(std::function<int()> get)
        : get(get)
    {}
    operator T()
    {
        return get();
    }
};

template<typename T>
class Vector
{
private:
    struct LinkItem
    {
    public:
        T* Item;
        LinkItem* Next;
        GetProperty<int> Length;
        LinkItem(T* Item = NULL, int length = 1, LinkItem* Next = NULL)
            : Length([this] { return this->getLength(); })
        {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM