Solved

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

Posted on 2010-11-26
7
851 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 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