开发者

C++ templates and structs problem

I have this...

template <typename Key, typename Value>
class A {
public:
    //...
private:
    struct MyStruct{
        Key key;
        Value value;
    };
};

And it gives me the following errors:

Error   1   error C214开发者_运维技巧6: syntax error : missing ';' before identifier 'value' 
Error   2   error C4430: missing type specifier - int assumed.
Error   3   error C4430: missing type specifier - int assumed.

Some specs: Using Visual Studio 2010 Windows 7 x64

Can my error be before or after these lines?


You forgot a semicolon after the class definition, at the very end of your code.

In some cases you need to write typename Key key; instead of a simple Key key; in templates, since the compiler may not know that Key is really a typename. So try this:

template <typename Key, typename Value>
class A {
public:
    //...
private:
    struct MyStruct {
        /* typename not allowed here */ Key key;
        /* typename not allowed here */ Value value;
    };
};


You forgot the last semi-colon to end your class definition.


It looks like you missed a semi-colon to close out your class.

This compiles fine for me using g++ 4.6.1:

#include <iostream>

template <typename Key, typename Value>
class A {
  public:
    A() {}
  private:
    struct MyStruct {
        Key key;
        Value value;
    };
};

int main(void) {
    A<int, char> a;
}


First, maybe it's just a typo in the code you pasted, but you are missing the ; at the end of the class definition.

Beyond that, I don't see a problem. Make sure you are declaring an object as A<type, type> a;

What are you using for your template types?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜