0

I am new to c++, and reying to use templates. I have a function which can get some types.

header:

template <typname T>
response_t send_sequence_to_device(map<const string_t, T*> msg2device_p,
    vector<response_t>& result_list, ushort num_attempts=SEND_NUM_ATTEMPTS);

Source:

template <typname T>
response_t send_sequence_to_device( map<const string_t, T*> msg2device_p,
    vector<response_t>& result_list, ushort num_attempts )
{
    bool is_ok_flag = true;
    response_t r;
    raftor1_logs_t* rlogs;
    map<const string_t, T*>::iterator msg_it;
    for( msg_it=msg2device_p.begin(); msg_it!=msg2device_p.end() and is_ok_flag; msg_it++ )
    {
        r = msg_it->second->send(msg_it->first, true, num_attempts);
        result_list.push_back(r);
        is_ok_flag = is_ok_flag and is_ok(r);

        if( not(is_ok_flag) )
        {
            stringstream ss;
            ss << "ALERT: Sequence aborted due to error on message [" << msg_it->first << "] ";
            if( r.erred() )
                ss << "due to communication failure.";
            else
                ss << "with error message [" << r.msg << "].";
            rlogs->alert.begin_record();
            rlogs->alert.write( ss.str() );
            rlogs->alert.end_record();
        }
    }

    if( is_ok_flag )
        r.set_ok("ok.\n");

    return r;
}

I get the following error:

device_manager.cpp|1076|error: need 'typename' before 'std::map, T*>::iterator' because 'std::map, T*>' is a dependent scope

6
  • 1
    Assuming the adequate typedefs are visible from this prototype, it looks fine. Please post a complete code snippet that produces this error.
    – Quentin
    May 4, 2016 at 9:25
  • @Quentin- this is the relevant code in the header. Do you need the code from the Source?
    – Sarit8
    May 4, 2016 at 9:30
  • 1
    do you have template <class T> in the source?
    – kmdreko
    May 4, 2016 at 9:34
  • @Sarit8 Not necessarily, but enough of your code that we can compile and see the error. The best way to do that is with an online compiler such as Coliru. If for some reason this isn't possible, you should provide as much information as possible : where exactly the error happens, what all of the involved types are, etc.
    – Quentin
    May 4, 2016 at 9:36
  • @vu1p3n0x- no, I don't have.
    – Sarit8
    May 4, 2016 at 10:09

1 Answer 1

1

Here:

map<const string_t, T*>::iterator msg_it;

You need this:

typename map<const string_t, T*>::iterator msg_it;
6
  • I can also read the error:) But why do I need the typename, and why only in the source?
    – Sarit8
    May 4, 2016 at 11:07
  • 1
    You need typename because the compiler can't know in advance if map<string, T>::iterator is a type or a value. C++ requires that in such cases the compiler should assume it's a value, unless you explicitly say it's a typename. And why in this case, well that's because it has the form klass<T>::type, where T is a template argument in the enclosing scope. May 4, 2016 at 11:09
  • but why I don't have to write is also in the heder file?
    – Sarit8
    May 4, 2016 at 11:19
  • @Sarit8: Because you never had the pattern klass<T>::type in the header. Only klass<T> which doesn't need typename. May 4, 2016 at 11:20
  • 1
    @Sarit8 John is referring to map<const string_t, T*>::iterator. The type iterator depends on the template map<const string_t, T*>, so it is a dependent type. The error is only generated in a cpp file because the template is instantiated when it is used. When the compiler first sees a template function, it does only very limited checking because it does not know what T is. When you then use the template somewhere, it does the full syntax check and generates the error.
    – Jens
    May 4, 2016 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.