English 中文(简体)
c++ 禁止宣布无种类的通用
原标题:iso c++ forbids declaration of generic with no type
  • 时间:2012-05-25 15:53:28
  •  标签:
  • c++
  • generics

我的母语是C##, 所以当我开始使用C++时, 我想为 C# 中的图书馆用户创建获取/设置食糖语法。

于是我写了...

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;
        }
 };

然后,为了使用这个,我写了代码:

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?

最佳回答

除了 VC++ 不执行非静态数据成员初始化器之外, 您不能将成员函数Length 处理为 int (*)() 。 它的类型是 int (LinkTrom:*)()

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

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

您可能不应该试图导入像这样的外国语言, 但是如果您真的想要, 您可以尝试一些类似的东西。 (尽管正如 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(); })
        {
...
问题回答

您正在建造 GetProperty 对象时, 您正在以支架声明它。 在 C++ 中不允许这样做。 您必须将构造移动到构建器中 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签