Solved

c++ pointer to boost::multi_array is not recognized

Posted on 2010-11-26
7
838 Views
Last Modified: 2012-05-10
Hi,

I am trying to pass pointer to an array (boost::multi_array) within a class. It is not accepted by the compiler:

No matching function for getImage2(boost::multi_array<double, 2u,std::allocator<double>>*, const char*)

=================
#include <QtCore/QCoreApplication>
#include <boost/multi_array.hpp>

#include <iostream>
#include <fstream>

template<typename T, int rank>
void getImage2(boost::multi_array<T, rank> *Image, char* filename)
{
    int size = sizeof(T) * (Image.num_elements());
    ifstream myFile;
    myFile.open(filename, ios::in | ios::binary);
    if (!myFile.is_open()) {cerr <<"getImage: file not open\n"; }
    myFile.read((char*)&Image,size);
    myFile.close();
    return;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    boost::multi_array<double,2> Image(boost::extents[192][128]);
    string filename2 = "double.bin";
    getImage2(&Image,filename2.c_str());

    return a.exec();
}
0
Comment
Question by:jhalsadah
  • 4
  • 2
7 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 400 total points
ID: 34220864
The problem is two-fold :

(a) The function needs to take a const char* as second parameter :

        template<typename T, int rank>
        void getImage2(boost::multi_array<T, rank> *Image, const char* filename)

(b) The function call should explicitly state the template parameters :

        getImage2<double, 2>(&Image,filename2.c_str());

And that should get you past the error you had.

Note also that inside the getImage2 function, you are trying to use the Image parameter as if it were a boost::multi_array object, not a pointer.
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 100 total points
ID: 34221647
"The function call should explicitly state the template parameters"

this is not always necessary if the compiler can deduce the template arguments by the usage and context of the template function call.

Just wanted to make this clear.

http://www.cplusplus.com/doc/tutorial/templates/
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34221658
Right, but that's not the case here :) So you should explicitly state them, as I said.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 34221673
"Right, but that's not the case here"

hmm i wouldn't be so sure of this even for this case. This exactly one of the cases the VS2010 compiler is targeting...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 400 total points
ID: 34221834
>> hmm i wouldn't be so sure of this even for this case. This exactly one of the cases the VS2010 compiler is targeting...

Have you actually tried it ? I would be surprised if it works. And if it does, then that's an extension of the C++ standard.

The problem in this specific case is that the template parameter types of boost::multi_array don't match those of the getImage2 function. Specifically, the second template argument of boost::multi_array is a std::size_t, and not an int.

So, the function template arguments cannot be deduced.

If the function would have been defined like this instead, things would have been different :

        template<typename T, std::size_t rank>
        void getImage2(boost::multi_array<T, rank> *Image, const char* filename)

then you wouldn't have to explicitly state the template parameters when calling the function in the way shown in the question.

But, as the question stands, they are required (unless the function template arguments can be changed as shown above).
0
 

Author Comment

by:jhalsadah
ID: 34222974
Great thanks to both of you,

I have used Blitz++ array in the past, with g++ compiler and template calling by integer was fine.
template<typename T, int rank>
void getImage2(array<T, rank> *Image, char* filename)

Now with Blitz++ and the "new" g++  the function should be:

template<typename T, std::size_t rank>
void getImage2(boost::multi_array<T, rank> *Image, const char* filename)

and the calling should be
getImage2(&Image, filename.c_str());

This is valid and works. One more question: would passing the array itself instead of a pointer would create another instance of the array and waste memory? i.e.

template<typename T, std::size_t rank>
void getImage2(boost::multi_array<T, rank> Image, const char* filename)

called by
getImage2(Image, filename.c_str());

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34222992
>> I have used Blitz++ array in the past, with g++ compiler and template calling by integer was fine.

It depends on what the template parameter types of array are :)


>> One more question: would passing the array itself instead of a pointer would create another instance of the array and waste memory?

It could.

Why not pass it by reference ?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now