Solved

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

Posted on 2010-11-26
7
848 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 72
Embarcadero C++ Builder XE2 TDateTime 8 71
Issues with C++ Class 19 92
show out valin of json for debugging in visaul c++ 1 33
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

810 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