dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Boost multi_array troubles and general C++ frustrations

  1. #1
    Join Date
    Jan 2011
    Posts
    20

    Boost multi_array troubles and general C++ frustrations

    EDIT - Lindley's sage advice saved the day. Here's the breakdown:
    1) I'm an idiot and tried to instantiate a member variable inside my class definition.
    2) Boost multi_array doesn't play well with VC++10. In the Debug configuration, you'll get an iterator error from within VC source when using it. The fix for this is to add _ITERATOR_DEBUG_LEVEL=0 to the preprocessor definitions for Debug (or all) configurations.
    Again, thanks to Lindley for this, who also warns that when using the above workaround: "you may have trouble linking to external libraries which are not built with this symbol!"

    Original Post follows:
    =================

    [First post. Ranting a bit at the beginning, so skip to the code if you don't want to read my whining]

    So I've been out of the programming loop for about a year. When I last did C++ programming I had the good fortune to have a supervisor who could decipher the seemingly meaningless compile errors I would often get.

    Now that I'm trying to get back into it, I don't have that advantage, and I'm regularly pulling my hair out in frustration because I get totally unhelpful error messages that also happen to be so vague that googling them gets me nowhere at all.

    So my first and decidedly more important question is, can anyone point me at a knowledge base or "cheat sheet" page of some sort that'll help me keep my sanity? The other day after 3 hours of desk-pounding I remembered that a syntax error in a header file could cause an error in my .cpp file that's error-free.

    Right how I'm having a problem getting the multi_array from boost libraries to work. Just importing boost I found that VC++10 couldn't find any files deeper than the "boost/multi_array.hpp" file I explicitly included (errors that "boost/detail/whatever.hpp" doesn't exist). To fix this, I changed (literally) every #include in the boost headers from <file.hpp> to "file.hpp". I'm certain there was an easier way to do it, but even though I know tens of thousands of people must've used boost in VC++10, I found no evidence of this problem on the web.

    That seems to have fixed the immediate problem, and now I've got compile errors no matter how I try to make a multi_array.

    ===========
    #ifndef _GRID_H
    #define _GRID_H

    #include "Cell.h"
    #include "boost/multi_array.hpp"

    class Grid {
    public:
    int xSize, ySize;
    // Just make a 1x1 grid for now. We'll resize it in the constructor.
    typedef boost::multi_array<double, 3> array_type;
    boost::array<array_type::index, 3> shape = {{ 3, 4, 2 }};
    array_type A(shape);


    Grid(int xSize, int ySize);
    ~Grid();

    void gridInit(Grid * grid);
    };

    #endif
    ============

    The above utilizes a direct copy from one of the boost examples. I tried many different ways and eventually resorted to copying and pasting. The error I get is:
    1>c:\...\grid.h(12): error C2059: syntax error : '{'

    I also tried another adaptation of example code:
    boost::multi_array<Cell *, 2> cells(boost::extents[1][1]);

    I get this error:
    1>c:\...\grid.h(11): error C2061: syntax error : identifier 'extents'

    This:
    boost::array<multi_array_base::index,3> my_extents = {{1, 1}};
    boost::multi_array<Cell *, 2> cells(my_extents);

    ...gets me this:
    1>c:\...\grid.h(11): error C2653: 'multi_array_base' : is not a class or namespace name


    I think I tried 2 or three other examples and they all gave me errors pointing to a namespace or syntax problem.

    Here's the boost multi_array reference:
    http://www.boost.org/doc/libs/1_45_0.../doc/user.html


    I was never much good at C++, but I still feel like when I program in C 95&#37; of my problems are resolved by just looking at the compiler error and maybe backtracking a few times. With C++ it seems closer to 30%. Am I missing something? Can someone help me with this specific problem.


    Thanks guys, sorry for the short novel here. This isn't the best place to vent, I know. I'm hoping someone has been in a similar overall situation before and can give me some direction.

    - Glen
    Last edited by gkimsey; January 19th, 2011 at 08:12 PM.

  2. #2
    Join Date
    Jan 2011
    Location
    Richmond, Va
    Posts
    32

    Re: Boost multi_array troubles and general C++ frustrations

    Ok well I just checked out boost and while I can't say anything specifically about your code, just make sure you fully understand the scope resolution operator and templates...aswell as all the basic c/c++ syntax.

    Boost looks as if it uses a completely different style than usual c++ code. O_o

    -edit-

    The library you're trying to use is based on meta programming which looks extremely complicated. Consider getting a book:

    http://www.amazon.com/Template-Metap.../dp/0321227255

    Good luck.
    Last edited by Amaz1ng; January 19th, 2011 at 04:37 PM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Boost multi_array troubles and general C++ frustrations

    Code:
    ===========
    #ifndef _GRID_H
    #define _GRID_H
    
    #include "Cell.h"
    #include "boost/multi_array.hpp"
    
    class Grid {
    public:
    	int xSize, ySize;
    	// Just make a 1x1 grid for now.  We'll resize it in the constructor.
    	typedef boost::multi_array<double, 3> array_type;
    	boost::array<array_type::index, 3> shape = {{ 3, 4, 2 }};
    	array_type A(shape);
    
    
    	Grid(int xSize, int ySize);
    	~Grid();
    
    	void gridInit(Grid * grid);
    };
    
    #endif
    First off, you can't initialize in a class definition (except for a few very specific cases), so lose the "= {{ 3, 4, 2}}" bit. I'm not even sure what you're trying to do there; you have an array of 3 3D multi_arrays. Why not simply make a 4D multi_array instead?

    There is one thing I need to warn you about. As of the last time I checked, there was still some kind of iterator problem with the multi_array library on Visual Studio 2010. You'll only see it in Debug builds, not Release builds, and there is a workaround; just letting you know.
    Last edited by Lindley; January 19th, 2011 at 05:24 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Boost multi_array troubles and general C++ frustrations

    Quote Originally Posted by Amaz1ng View Post
    Ok well I just checked out boost and while I can't say anything specifically about your code, just make sure you fully understand the scope resolution operator and templates...aswell as all the basic c/c++ syntax.

    Boost looks as if it uses a completely different style than usual c++ code. O_o

    -edit-

    The library you're trying to use is based on meta programming which looks extremely complicated. Consider getting a book:

    http://www.amazon.com/Template-Metap.../dp/0321227255

    Good luck.
    Template metaprogramming is difficult to do, but once it's done the resulting library should be easy to use----which it is, in the case of most Boost libraries. Don't try to read the code yourself, just stick to the documentation and you'll be fine.

    The code behind the Standard Template Library is just as obscure as that in Boost, but it's widely agreed that the STL makes your life as a coder much easier. Same thing.
    Last edited by Lindley; January 19th, 2011 at 05:29 PM.

  5. #5
    Join Date
    Jan 2011
    Posts
    20

    Re: Boost multi_array troubles and general C++ frustrations

    Quote Originally Posted by Lindley View Post
    First off, you can't initialize in a class definition (except for a few very specific cases), so lose the "= {{ 3, 4, 2}}" bit. I'm not even sure what you're trying to do there; you have an array of 3 3D multi_arrays. Why not simply make a 4D multi_array instead?

    There is one thing I need to warn you about. As of the last time I checked, there was still some kind of iterator problem with the multi_array library on Visual Studio 2010. You'll only see it in Debug builds, not Release builds, and there is a workaround; just letting you know.
    You managed to nail both of my problems in one reply, I think.

    I failed to mention what started me on this path in the first place which is this error:
    Code:
    1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(2216): error C2665: 'std::_Copy_impl' : none of the 2 overloads could convert all the argument types
    which comes from this line:
    Code:
    c:\...\grid.h(10) : see reference to class template instantiation 'boost::multi_array<T,NumDims>' being compiled
    1>          with
    1>          [
    1>              T=Cell *,
    1>              NumDims=2
    1>          ]
    when I use
    Code:
    boost::multi_array<Cell *, 2> cells;
    in the class definition like I'm supposed to (good call on that whole trying to instantiate in the class definition problem. I got tunnel vision trying to figure this out and didn't even know what I was doing. That explains the syntax error, although I still contend the error could easily be a lot more helpful).

    I'm *guessing* this is the boost and VC++10 problem you mentioned. I got a few results from google on it but nothing definitive. One thread said Microsoft had already fixed the bug, so now I'm wondering if it's still my fault.

    If not, though, leave it to me to find a bug (?) in VC++10 within the first few hours of using it.

    Thanks, and let me know if you have any more info on the boost workaround... I couldn't find it, and might be looking in the wrong place.

  6. #6
    Join Date
    Jan 2011
    Posts
    20

    Re: Boost multi_array troubles and general C++ frustrations

    Quote Originally Posted by Lindley View Post
    Template metaprogramming is difficult to do, but once it's done the resulting library should be easy to use----which it is, in the case of most Boost libraries. Don't try to read the code yourself, just stick to the documentation and you'll be fine.

    The code behind the Standard Template Library is just as obscure as that in Boost, but it's widely agreed that the STL makes your life as a coder much easier. Same thing.
    Thank you for this.
    There may come a day when I can actually read the other 3/4 of the boost libraries, but that day is a long way off.

  7. #7
    Join Date
    Jan 2011
    Posts
    20

    Re: Boost multi_array troubles and general C++ frustrations

    Oh, I did go back to that original problem and try Release build. Sure enough, it compiles.

    Also, what you were seeing me "try to do" there was just shove an example into the class definition. What I *actually* want to do is just use a 2D array of Cell pointers within the Grid class without losing the ability to use the cells[x][y] syntax and while gaining the ability to resize the array. I'm wondering if STL wouldn't be better for that anyway.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Boost multi_array troubles and general C++ frustrations

    Well, a vector<vector<Cell*>> (or better yet, use smart pointers) will provide a similar functionality but will have a different layout in memory. The upshot is that it will allow you to resize each row individually; the downside is it requires more allocations and can be more difficult to manage if you are only interested in rectangular arrays.

    The workaround to get multi_array working with Debug mode on VS2010 is to add the preprocessor symbol _ITERATOR_DEBUG_LEVEL=0. You can do this under Project Properties->C/C++->Preprocessor. Be warned, you may have trouble linking to external libraries which are not built with this symbol!

  9. #9
    Join Date
    Jan 2011
    Posts
    20

    Re: Boost multi_array troubles and general C++ frustrations

    Excellent! Problem resolved. I think I'll stick to multi_array unless it gives me more problems. I like the interface, and given how much I'm going to be messing with the "grid," that's pretty important.

    Thanks a ton for your help.

    By the way, can anyone tell me how to edit posts? I must be blind. I'd like to edit the original post so others can find the answers easier.

    EDIT - Never mind, the button showed up. Not sure why it wasn't there before.

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Boost multi_array troubles and general C++ frustrations

    Quote Originally Posted by gkimsey View Post
    EDIT - Never mind, the button showed up. Not sure why it wasn't there before.
    You only get the edit button after doing some posts. IIRC three are required.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)