Solved

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

Posted on 2010-11-26
7
841 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 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

18 Experts available now in Live!

Get 1:1 Help Now