locked
Declaring vectors RRS feed

  • Question

  • I'm working on the development of a strategy game wherin specific characters could have other characters of the same type as followers. So obviously I need an array or vector embedded in the Character class. This is what I've got:

     

    #include <vector>

    ...

    std::vector<Character> followers;

     

    This is running into all sorts of problems, though:

     

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xmemory(96) : error C3699: '*' : cannot use this indirection on type 'SpellforgedForms::Character'

    compiler replacing '*' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(416) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled

    with

    [

    _Ty=SpellforgedForms::Character

    ]

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(426) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled

    with

    [

    _Ty=SpellforgedForms::Character,

    _Alloc=std::allocator<SpellforgedForms::Character>

    ]

    c:\documents and settings\micahbickford\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : see reference to class template instantiation 'std::vector<_Ty>' being compiled

    with

    [

    _Ty=SpellforgedForms::Character

    ]

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xmemory(97) : error C3699: '&' : cannot use this indirection on type 'SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xmemory(98) : error C3699: '*' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '*' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xmemory(99) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xmemory(154) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(473) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(479) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(531) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(785) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(846) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(851) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(858) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1058) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1116) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1206) : error C3699: '&' : cannot use this indirection on type 'const SpellforgedForms::Character'

    compiler replacing '&' with '^' to continue parsing

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1243) : error C3265: cannot declare a managed '_Myfirst' in an unmanaged 'std::vector<_Ty>'

    with

    [

    _Ty=SpellforgedForms::Character

    ]

    may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1244) : error C3265: cannot declare a managed '_Mylast' in an unmanaged 'std::vector<_Ty>'

    with

    [

    _Ty=SpellforgedForms::Character

    ]

    may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap

    C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1245) : error C3265: cannot declare a managed '_Myend' in an unmanaged 'std::vector<_Ty>'

    with

    [

    _Ty=SpellforgedForms::Character

    ]

    may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap

    c:\documents and settings\micahbickford\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : error C4368: cannot define 'followers' as a member of managed 'SpellforgedForms::Character': mixed types are not supported

     

    If I comment out the "#include" line, my error list is significantly smaller:

     

    c:\documents and settings\...\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : error C2039: 'vector' : is not a member of 'std'

    c:\documents and settings\...\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : error C2143: syntax error : missing ';' before '<'

    c:\documents and settings\...\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    c:\documents and settings\...\my documents\visual studio 2005\projects\spellforgedforms\spellforgedforms\Character.h(60) : error C2238: unexpected token(s) preceding ';'

     

    So I've come to the conclussion that either my declaration of the vector is wrong, or that you can't have a vector within an object that includes to instances of that same type of object.

    Sunday, June 3, 2007 1:09 PM

Answers

  • You should probably keep a vector of pointers to Character, rather than the Characters themselves. Not using pointers will cause the Character instances to be copied whenever you assign or extract them to/from the vector.

     

    Code Snippet

    #include <vector>

     

    class Character {}; <= Just a dummy. Use your own declaration, which is probably included through some other header.

     

    std::vector<Character*> charVec;

     

    That should work just fine.

     

    By the way, are you trying to use C++/CLI or native C++? I noticed the '^' in your error list -- which is a managed reference.

    Sunday, June 3, 2007 1:56 PM

All replies

  • You should probably keep a vector of pointers to Character, rather than the Characters themselves. Not using pointers will cause the Character instances to be copied whenever you assign or extract them to/from the vector.

     

    Code Snippet

    #include <vector>

     

    class Character {}; <= Just a dummy. Use your own declaration, which is probably included through some other header.

     

    std::vector<Character*> charVec;

     

    That should work just fine.

     

    By the way, are you trying to use C++/CLI or native C++? I noticed the '^' in your error list -- which is a managed reference.

    Sunday, June 3, 2007 1:56 PM
  • or that you can't have a vector within an object that includes to instances of that same type of object.

    You certainly can't; at the point where you try to include a vector of instances of the class, the class is still an "incomplete type", so the compiler has no idea how to set up the vector.

    When you run into a "it can't be done" roadblock like this, it's frequently a not-so-subtle hint that you may want to re-think the design.

    Monday, June 4, 2007 12:17 AM
  • I doubt your project is of C++/CLI type?
    Monday, June 4, 2007 5:15 AM
  •  Sdi wrote:

    or that you can't have a vector within an object that includes to instances of that same type of object.

    You certainly can't; at the point where you try to include a vector of instances of the class, the class is still an "incomplete type", so the compiler has no idea how to set up the vector.

    When you run into a "it can't be done" roadblock like this, it's frequently a not-so-subtle hint that you may want to re-think the design.

     

    I can't remember seeing any compiler complain about this. Either way, using pointers would clear up that problem, if it's even a problem.

    Monday, June 4, 2007 7:07 AM
  • If you ever compile a class that contains (directly or indirectly) an instance of itself (as opposed to a pointer or a reference to itself), please let us know, since that is specifically disallowed by the language standard:

    "Non-static (9.4) data members shall not have incomplete types. In particular, a class C shall not contain a non-static member of class C, but it can contain a pointer or reference to an object of class C."

    Monday, June 4, 2007 8:25 PM
  • I'd say this depends on the STL implementation more than the language. Most implementations probably either store a reference or pointer to the class at some depth, rather than a copy of the class itself, and hence aren't troubled by partial implementations. Keeping a vector of Foo within Foo itself certainly doesn't fail very often. As I said, I've never seen a compiler complain about it -- not even strict Comeau.
    Monday, June 4, 2007 9:07 PM
  • i am having a problem while executing a program. i have created a new class where i have declared vector of vectors. the compiler is not letting me to execute it showing some errors...

    #pragma once
    #include "vector"

    ref class CNetworkInfo
    {
        int numNodes;
        int numLinks;
        vector< vector<int> > adjMatrix;
    public:
        CNetworkInfo(void);
        CNetworkInfo(int, int);
        //vector<vector<int>> initialize(vector< vector<int> >, int);
        //vector<vector<int>> reinitialize(vector< vector<int> >, int);
    };

    the errors that the compiler showed are
    Error    1    error C2143: syntax error : missing ';' before '<'   
    Error    4    error C2143: syntax error : missing ';' before '<'   
    Error    3    error C2238: unexpected token(s) preceding ';'   
    Error    6    error C2238: unexpected token(s) preceding ';'  
    Error    2    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
    Error    5    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

    now if i uncomment this line it still shows some more errors
    //vector<vector<int>> initialize(vector< vector<int> >, int);

    can any one please help me regarding this..........

    • Edited by shumi22 Tuesday, June 24, 2008 7:37 PM spelling mistake
    Tuesday, June 24, 2008 7:37 PM
  • You have created a mixed type (that is, a native member belonging to a managed class). This is not permitted in VS 2005 or VS 2008.

    I'm also guessing that you forgot to use the std namespace.

    You could change it to:

    std::vector<std::vector<int> > *adjMatrix;

    That is, a pointer is allowed.

    You can read more about the nitty gritty here

    In future, it is better to start a new thread, since your post really doesn't have anything to do with the earlier part of the thread.

    Tuesday, June 24, 2008 9:02 PM
  • thank you so much for your help......
    my code is working fine with your suggestions.......
    Wednesday, June 25, 2008 3:21 PM