Solved

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

Posted on 2010-11-26
7
856 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

623 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