dimanche 4 septembre 2022

editor recognizes type, but compiler does not

i split a class into two classes, now the editor recognizes types, but compiler does not.

CControls.h

class CControls : public QThread
{
    CControls();
}

CThreadController.h

#include "CControls.h"

class CThreadController : public QThread
{
    Q_OBJECT
    CControls *m_controls;
}

error

/home/michaeleric/Qt_Creator_Projects/GUI/CThreadController.h:22: error: ‘CControls’ does not name a type
22 |         CControls *m_controls;
  |         ^~~~~~~~~

i dont get how the editor sees CControls as a type, but compiler does not. i dont know qhat else to look at or for.

what is going on?

Valgrind error: Conditional jump/uninitialised value(s) using Eigen in C++

Usually, I am fairly comfortable with Valgrind error messages when using C++ arrays and not with Eigen matrices. This error means to be something is not initialized, even though I cannot see how is that the case. Seems like valgrind shows the place where the unitialised value ends up being used, but not the origin of the uninitialised value. I am using the flag:

valgrind --track-origins=yes

which usually helps identifying the lines, but for some reason this isn't working with Eigen. The full error messages: Error1:

 Conditional jump or move depends on uninitialised value(s)
==7674==    at 0x13BCFD: Eigen::internal::triangular_solve_vector<std::complex<double>, std::complex<double>, long, 1, 2, false, 0>::run(long, std::complex<double> const*, long, std::complex<double>*) (in /mnt/c/Users/J/Desktop/Data/test.out)
==7674==    by 0x13C831: Eigen::internal::triangular_solver_selector<Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, -1, -1, false> const, Eigen::Block<Eigen::Matrix<std::complex<double>, -1, 1, 0, -1, 1>, -1, 1, false>, 1, 2, 0, 1>::run(Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, -1, -1, false> const&, Eigen::Block<Eigen::Matrix<std::complex<double>, -1, 1, 0, -1, 1>, -1, 1, false>&) 

Error 2:

  by 0x13CB4C: void Eigen::ColPivHouseholderQR<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> >::_solve_impl<Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, -1, 1, true>, Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, -1, 1, true> >(Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, -1, 1, true> const&, Eigen::Block<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, -1, 1, true>&) const (in /mnt/c/Users/J/Desktop/Data/test.out)
==7674==    by 0x12F80F: potentialk(Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, 0, Eigen::OuterStride<-1> >, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::InnerStride<1> >, Eigen::Ref<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::InnerStride<1> >, Eigen::Ref<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, double, int)

Error 3:

==7674==    by 0x12F80F: potentialk(Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1>, 0, Eigen::OuterStride<-1> >, Eigen::Ref<Eigen::Matrix<std::complex<double>, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, Eigen::Ref<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::InnerStride<1> >, Eigen::Ref<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::InnerStride<1> >, Eigen::Ref<Eigen::Matrix<double, -1, -1, 0, -1, -1> const, 0, Eigen::OuterStride<-1> > const&, double, int)

These errors sort of just repeat, so the first one doesn't make sense to me. But, the second and third I can guess what line of code they're referring to but I don't see where things are uninitialized??

The error points first at the function definitions:

int potentialk(const Eigen::Ref<const Eigen::MatrixXcd>& invnk, const Eigen::Ref<const Eigen::MatrixXcd>& dndxk, const Eigen::Ref<const Eigen::MatrixXcd>& dndyk, Ref<MatrixXcd> phik, const Eigen::Ref<const Eigen::MatrixXcd>& potSourcek, Ref<VectorXd> kx,Ref<VectorXd> ninvksqu,const Eigen::Ref<const Eigen::MatrixXd>& D, double err_max, int max_iter){

Then inside in a while loop:

//Initializations
Eigen::MatrixXcd L((ny+1),(ny+1));
L.setZero();

Eigen::SparseMatrix<double> Igl((ny+1),(ny+1)); 
Igl.setIdentity();
    
Eigen::SparseMatrix<double> div_x_act_on_grad_x((ny+1),(ny+1)); 
div_x_act_on_grad_x.setIdentity();
div_x_act_on_grad_x = -Igl;

Eigen::MatrixXd div_y_act_on_grad_y((ny+1),(ny+1));
div_y_act_on_grad_y.setZero(); 

Eigen::MatrixXcd Stilde((ny+1),nx);
Stilde.setZero();
do{
//some calculations
for (int l = 0; l < kx.size(); l++){
   L = div_x_act_on_grad_x * std::pow(ninvksqu(l),2.0) + div_y_act_on_grad_y; 
  unewh(all,l) = L.cast<std::complex<double>>().colPivHouseholderQr().solve(Stilde(all,l));//ERROR
}

}while(); //convergence condition

Is it possible this error refers to the way some variables are defined or passed in the function?

samedi 3 septembre 2022

C++ How to solve "no matching function for call" in class with template [closed]

I have a class called Thingy which has a member function dosomething which takes another Thingy as input. I can't figure out why I can't invoke the function. I'm sure its something dumb I've done but I can't see it. Using C++11

In thingy.h I have:

template<typename T>
class Thingy {
    ...

    /**
     * Returns the result as a new instance
     */
    Thingy<T> dosomething(const Thingy<T>& thing) const {
        return dosomething(thing, DEFAULT_THRESHOLD);
    }

    /**
     * Returns the result as a new object
     */
    Thingy<T> dosomething(const Thingy<T>& thing, const unsigned threshold) const {
    ...
    }
}

Appreciate any help, thank you!

p01core.cc:82:144:   required from here
p01core.cc:65:34: error: no matching function for call to ‘Thingy<int>::dosomething(Thingy<int>*, unsigned int&)’
                     result= thing1.dosomething(&thing2, threshold);
In file included from p01core.h:16,
                 from p01core.cc:9:
thingy.h:164:19: note: candidate: ‘Thingy<T> Thingy<T>::dosomething(const Thingy<T>&) const [with T = int]’
         Thingy<T> dosomething(const Thingy<T>& B) const {
                   ^~~~~
thingy.h:164:19: note:   candidate expects 1 argument, 2 provided
thingy.h:171:19: note: candidate: ‘Thingy<T> Thingy<T>::dosomething(const Thingy<T>&, unsigned int) const [with T = int]’
         Thingy<T> dosomething(const Thingy<T>& thing, const unsigned threshold) const {
                   ^~~~~

jeudi 1 septembre 2022

How can I make std::thread not struck the pragma?

I want to design a timer class, there is a function, which sleep some seconds, then call other function.

please see the code:

#include <thread>
#include <iostream>

void func() { printf("timer thread function called\n"); }

class Timer {
 public:
  template <typename Fn> 
  void sleep_start(int sec, const Fn& f) {
    printf("sleep %d\n", sec);
    td_ = std::thread([sec, f]() { std::this_thread::sleep_for(std::chrono::seconds(sec)); f(); }); 
    if (td_.joinable()) td_.join();
  }
  std::thread td_;
};

class A { 
 public:
  void start() {
    t_.sleep_start(10, func);
    printf("start function\n");
  }
  Timer t_; 
};

int main() {
  A a;
  a.start();
}

this code can work well, but the sleep_start function stuck the program.

the real output is:

sleep 10
timer thread function called
start function

the ideal output is:

sleep 10
start function
timer thread function called

could you help on this? how to make the thread function not struck the program?

Question about decltype and `std::vector::operator[]`

Since there are two overloading functions for std::vector::operator[], one returns a reference, and the other one returns a const reference.

Why the type of reg_auid is char& other than const char&, which is indcated by vscode?

#include <vector>

int main()
{
    std::vector<char> data_array{100};

    const int IDX = 1;

    //why the type of reg_auid is char& other than const char&
    decltype(data_array[IDX]) reg_auid = data_array[IDX]; 
}

no matching function for call to ‘ ’ [duplicate]

Here's the definition of my class:

class F1Team {
    private:
        string name;
        string racer1;
        string racer2;

    protected:
        int titles;
    
    public:
        F1Team(string tnm) {
            name = tnm;
            titles = 0;
            
        }
        void set_racer1(string r1) {
            racer1 = r1;
        }
        void set_racer2(string r2) {
            racer2 = r2;
        }
        void display() {
            cout<< name<< endl;
        }
};

I want to store class objects mapping to an int (i.e., <int, [class]>)

Here's my code to implement it:

int main() {
    map<int, F1Team> mp;
    for (int i=0; i<3; ++i) {
        
        F1Team t {"Mercedes"};
        mp.insert(pair<int , F1Team>(i, t));
    }

    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        (*itr).second.display();
    }
    mp[1].display();
    return 0;
}

The code throws compilation error when I try to access the object by key.

Error:

In file included from /usr/include/c++/11/bits/stl_map.h:63,
                 from /usr/include/c++/11/map:61,
                 from test.cpp:3:
/usr/include/c++/11/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = F1Team]’:
/usr/include/c++/11/tuple:1813:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = F1Team]’
/usr/include/c++/11/ext/new_allocator.h:162:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >]’
/usr/include/c++/11/bits/alloc_traits.h:516:17:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, F1Team> > >]’
/usr/include/c++/11/bits/stl_tree.h:595:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’
/usr/include/c++/11/bits/stl_tree.h:612:21:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’
/usr/include/c++/11/bits/stl_tree.h:2431:33:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::const_iterator]’
/usr/include/c++/11/bits/stl_map.h:520:37:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = F1Team; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = F1Team; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
test.cpp:45:9:   required from here
/usr/include/c++/11/tuple:1824:9: error: no matching function for call to ‘F1Team::F1Team()’
 1824 |         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:17:9: note: candidate: ‘F1Team::F1Team(std::string)’
   17 |         F1Team(string tnm) {
      |         ^~~~~~
test.cpp:17:9: note:   candidate expects 1 argument, 0 provided
test.cpp:7:7: note: candidate: ‘F1Team::F1Team(const F1Team&)’
    7 | class F1Team {
      |       ^~~~~~
test.cpp:7:7: note:   candidate expects 1 argument, 0 provided
test.cpp:7:7: note: candidate: ‘F1Team::F1Team(F1Team&&)’
test.cpp:7:7: note:   candidate expects 1 argument, 0 provided

I don't understand why the constructor to my class is being called here.