0

After a whole day of trying to get the Dlib library to run in QT, I finally got in running and thought I was home free. I tried to call the r_squared function and test it on some vectors and it doesn't let me set it equal to a double x to print out, nor did it let me cout(qDebug() << ...) in QT, the function r_squared. Below I have copied my code and also the function from the header can anyone let me know not just what's I'm doing wrong here, but what's goin on? all header/sources have been included properly.

VECTORS:

std::vector<double> avec;
std::vector<double> bvec;

avec.push_back(1);
avec.push_back(2);
avec.push_back(3);
avec.push_back(4);
bvec.push_back(5);
bvec.push_back(6);
bvec.push_back(7);
bvec.push_back(1);

BUILDS AND RUNS OK:

double x;

double r_squared (
       const std::vector<double>& avec,
       const std::vector<double>& bvec
   );

DOES NOT WORK (both the assignment and qDebug() "cout in QT"

double x;
x = r_squared (
            const std::vector<double>& avec,
            const std::vector<double>& bvec
        );
qDebug() << x; 
qDebug() << r_squared(enter actual parameters)

erroe C2059 was recieved for the block above

CODE AS DEFINED FROM DLIB

  template <
    typename T, 
    typename alloc
    >

double r_squared (
    const std::vector<T,alloc>& a,
    const std::vector<T,alloc>& b
)
{
    // make sure requires clause is not broken
    DLIB_ASSERT(a.size() == b.size() && a.size() > 1,
                "\t double r_squared(a,b)"
                << "\n\t a and b must be the same length and have more than one element."
                << "\n\t a.size(): " << a.size()
                << "\n\t b.size(): " << b.size()
    );

    return std::pow(correlation(a,b),2.0);
}

also I just tried

x = r_squared (
       &avec,
       &bvec
   );

and received error message C3861: r_squared identifier not found. When I look in the header it is defined.

AND HERES THE TOTAL MAIN.CPP

#include "dlib3.h"
#include <QApplication>
#include <QDebug>
#include "dlib-master/dlib/statistics.h"


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
dlib3 w;

std::vector<double> avec;
std::vector<double> bvec;

avec.push_back(1);
avec.push_back(2);
avec.push_back(3);
avec.push_back(4);
bvec.push_back(5);
bvec.push_back(6);
bvec.push_back(7);
bvec.push_back(1);

double x;

x = r_squared (
       &avec,
       &bvec
   );



w.show();
return a.exec();
}
ASS466uiuc
  • 11
  • 1
  • 6

1 Answers1

1

Going through these one by one:

double r_squared (
       const std::vector<double>& avec,
       const std::vector<double>& bvec
   );

Does not call a function. It attempts to forward declare a r_squared function that is slightly different from the one in dlib/statistics.h. Compiles, but does not do anything at runtime. Not what you want to do.

x = r_squared (
        const std::vector<double>& avec,
        const std::vector<double>& bvec
    );

mixes function call and a function declaration syntax and is just plain incorrect. It doesn't do because, as OP has learned, it doesn't compile.

x = r_squared (
       &avec,
       &bvec
   );

calls r_squared with pointers to 2 vectors. This means the compiler goes looking for

template <typename T, typename alloc>
double r_squared (
    const std::vector<T,alloc>* a,
    const std::vector<T,alloc>* b
);

note the *s in place of the &s, and finds

template <typename T, typename alloc>
double r_squared (
    const std::vector<T,alloc>& a,
    const std::vector<T,alloc>& b
);

which doesn't match, so the compiler doesn't find r_squared. Not the r_squared you asked for at any rate.

Solution:

This has the smell of trying to learn C++ by blindly copying code without understanding what the code is doing, how, and why. Get a good book, read it, work through the examples, and learn C++. What OP is currently doing is wasting their time.

Then call the function without using the address-of operator on the parameters.

x = r_squared(avec, bvec);

Edit

As Evgeniy points out in a comment above, the dlib namespace also needs to be observed. The correct call would be

x = dlib::r_squared(avec, bvec);
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you, I actually did try the plain (avec, bvec) first, but forgot the namespace and moved on to less fruitful efforts as I saw the & in the definition. And, ya I kinda piecemealed my C++ together for a computational physics class last year and am now trying to solidify my skills. Thank you so much for the thorough explanation though. – ASS466uiuc Aug 26 '16 at 07:42