0

I require a function to print out the elements of a vector in a format that I'd like to see. As of now, I have separate functions for vector<double> and vector<std::complex>, as follows:

void printvd(boost::ublas::vector<double> V) {
    for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}
void printvc(boost::ublas::vector<std::complex> V) {
    for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}

I've tried templating with,

template<typename T>
void printv(boost::ublas::vector<T > V) {
    for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}

but any calls (like printv<double>(V);) invariably ends up with an undefined reference error.

I am facing similar issues with matrices too - any help would be appreciated. It would also be good if somebody could quip in about how to make use of matrix expressions in boost.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Nidish Narayanaa
  • 338
  • 3
  • 12
  • 1
    Undefined reference means you should *link* to boost when you compile. Also make sure you're putting the template function in your header file, not in your cpp source file. That's another possible cause of the issue. – The Quantum Physicist Mar 28 '17 at 08:00
  • Well, am working on a project with 5 cpp files (and corresponding headers) and using CMake for building. So I don't think the issue is with linkers. – Nidish Narayanaa Mar 28 '17 at 08:02
  • When you ask a question, learn to listen. Read the second part of my comment. – The Quantum Physicist Mar 28 '17 at 08:02
  • Undefined references to *what*? And you do know that [templates should be defined in the header files](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)? – Some programmer dude Mar 28 '17 at 08:05
  • [OT]: You should probably take your arguments by const reference instead of by value. – Jarod42 Mar 28 '17 at 08:20
  • And you don't have to specify the template type, compiler can deduce the type, so `printv(V);` should be enough. – Jarod42 Mar 28 '17 at 08:21
  • @TheQuantumPhysicist mybad, I put it in the header file and it works - Thanks! But elsewhere, I have templated a member function of a class. For this I used the declare-in-hpp-define-in-hpp route and it worked. Any pointers on why that was so? (Note that the class was not templated, only a member function was) – Nidish Narayanaa Mar 28 '17 at 08:42
  • @NidishNarayanaa Take a look here: http://stackoverflow.com/questions/18880700/creating-a-template-class-object-using-a-template-constructor/18880858#18880858 – The Quantum Physicist Mar 28 '17 at 08:44

1 Answers1

0

Please make sure you can compile this very basic example, where all paths&includes should be resolved.

#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#include <complex>

using std::cout;
using std::endl;

void printvd(boost::numeric::ublas::vector<double> V) {
    for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}
void printvc(boost::numeric::ublas::vector<std::complex<double>> V) {
    for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}

template<typename T>
void printv(boost::numeric::ublas::vector<T > V) {
  for( int i=0;i<V.size();i++ )
      std::cout << V(i) << " ";
}

int main(int argc, char *argv[])
{
  boost::numeric::ublas::vector<double> testVector1;
  boost::numeric::ublas::vector<std::complex<double>> testVector2;

  printv(testVector1);
  printv(testVector2);
  return 0;
}

And corresponding CMakeLists.txt:

project(boost_vector_test)
cmake_minimum_required(VERSION 2.8)
add_executable(${PROJECT_NAME} main.cpp)

Otherwise boost has good documentation with examples how to use matrix expressions: http://www.boost.org/doc/libs/1_63_0/libs/numeric/ublas/doc/matrix_expression.html

pe3k
  • 796
  • 5
  • 15