Thread: Dealing with '... is not a class, struct, or union type' error

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    17

    Dealing with '... is not a class, struct, or union type' error

    I have a function x_gradient() which creates a certain number of threads to carry out an edge detection task using the boost library. The threads each execute a function x_gradient_par() and this is where I'm having trouble.

    First, let me show you the block where I'm creating the threads:
    Code:
    template <typename SrcView, typename DstView>
    void x_gradient(const SrcView& src, const DstView& dst, int num_threads) {
        std::thread* mythreads = new std::thread[num_threads];
        int step = src.height() / num_threads;
        typedef typename channel_type<DstView>::type dst_channel_t;
    
        for(int i = 0; i < num_threads; i++){
            int start = step * i;
            int end = start;
            if(i == (num_threads - 1))
                end = src.height();
            else
                end = start + step;
            mythreads[i] = std::thread(&x_gradient_par<const SrcView&, const DstView&, const dst_channel_t&>, src, dst, start, end);
        }
        for(int i = 0; i < num_threads; i++){
            mythreads[i].join();
        }
    }
    This is what the thread function,x_gradient_par(), looks like:
    Code:
    template <typename SrcView, typename DstView, typename  dst_channel_t>
    void x_gradient_par(const SrcView& src, const DstView& dst, int start, int end) {
    
        for (int y = start; y < end; ++y)
        {
            typename SrcView::x_iterator src_it = src.row_begin(y); //error
            typename DstView::x_iterator dst_it = dst.row_begin(y); //error
    
            for (int x = 1; x < src.width() - 1; ++x)
            {
                static_transform(src_it[x - 1], src_it[x + 1], dst_it[x],
                                 halfdiff_cast_channels<dst_channel_t>());
            }
        }
    }
    When I compile, the error I receive is on lines
    Code:
    typename SrcView::x_iterator src_it = src.row_begin(y);
    and

    Code:
    typename DstView::x_iterator dst_it = dst.row_begin(y);
    and it looks something like this:
    const boost::gil::image_view<boost::........is not a class, struct, or union type

    The dots are there because the error is a lot of gibberish before it gets to the important part.

    I'm thinking it must have something to do with the way I'm passing src and dst to the thread function.

    Any ideas?

    I've attached a log file with the full error and the header file with the code.

    Thanks in advance!

    log.txt
    x_gradient_par.h

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    Regarding reference passing, have a look at std::ref/cref.

    Also keep in mind that you can use auto, unless you need to cast to a type different than the return value:
    Code:
    auto src_it = src.row_begin(y);
    auto dst_it = src.row_begin(y);
    To store the threads, prefer a container. Right now you're leaking memory:
    Code:
    std::vector<std::thread> mythreads;
    mythreads.reserve(num_threads);  // cannot hurt
    
    // ...
    
    mythreads.emplace_back(yourfunction, params);
    
    for (auto& t : mythreads) {
        t.join();
    }
    Last edited by -Adrian; 1 Week Ago at 05:30 PM.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    17
    Thanks auto fixed it.
    I also freed the memory I allocated for the threads. I'm wondering though is it generally more advantageous to use containers over arrays?

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    690
    I'm wondering though is it generally more advantageous to use containers over arrays?
    It's good practice to avoid new/delete where possible, and to rely on std::vector for dynamic arrays, std::unique_ptr/shared_ptr for singular objects on the heap. This helps reduce human error and the compiler will optimize virtually all those structures away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-04-2018, 05:54 PM
  2. error: invalid use of undefined type ‘struct tms’
    By DevZero in forum C Programming
    Replies: 1
    Last Post: 07-16-2017, 07:17 PM
  3. vector - left of .push_back must have class/struct/union type
    By patricio2626 in forum C++ Programming
    Replies: 5
    Last Post: 11-18-2006, 04:37 PM
  4. "ambiguous access to class/struct/union member"
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2003, 12:15 PM
  5. Class + Union + Struct = Disaster??
    By Inquirer in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:55 PM

Tags for this Thread