Cryptic error

N

none

In a class in some old legacy code I have:

template <class T>
void AddSeeds(const T& points) {
FOR_EACH_CONST(typename T, points, i) m_Seeds.push_back(*i);
}

when I compile I get the error:

error: expected nested-name-specifier before ‘T’
error: expected ‘(’ before ‘T’
error: ‘i’ was not declared in this scope


This very code has worked fine before without specifying a type for 'i'. Any ideas what: expected
nested-name-specifier before ‘T’ might mean in this context?
 
I

Ian Collins

In a class in some old legacy code I have:

template <class T>
void AddSeeds(const T& points) {
FOR_EACH_CONST(typename T, points, i) m_Seeds.push_back(*i);
}

when I compile I get the error:

error: expected nested-name-specifier before ‘T’
error: expected ‘(’ before ‘T’
error: ‘i’ was not declared in this scope


This very code has worked fine before without specifying a type for 'i'.
Any ideas what: expected nested-name-specifier before ‘T’ might mean in
this context?

The question isn't very helpful without the definition of FOR_EACH_CONST.
 
J

Jonathan Lee

In a class in some old legacy code I have:

        template <class T>
        void AddSeeds(const T& points) {
          FOR_EACH_CONST(typename T, points, i) m_Seeds.push_back(*i);
        }

when I compile I get the error:

error: expected nested-name-specifier before ‘T’
error: expected ‘(’ before ‘T’
error: ‘i’ was not declared in this scope

This very code has worked fine before without specifying a type for 'i'. Any ideas what: expected
nested-name-specifier before ‘T’ might mean in this context?

What does the expansion of FOR_EACH_CONST look like?

And why would you use a macro here anyway? You're
already templating. What's wrong with:

    template <class T>
    void AddSeeds(const T& points) {
for (typename T::const_iterator it = points.begin(); it !=
points.end(); ++it)
m_Seeds.push_back(*it);
    }


--Jonathan
 
N

none

Jonathan said:
What does the expansion of FOR_EACH_CONST look like?

And why would you use a macro here anyway? You're
already templating. What's wrong with:

template <class T>
void AddSeeds(const T& points) {
for (typename T::const_iterator it = points.begin(); it !=
points.end(); ++it)
m_Seeds.push_back(*it);
}


--Jonathan

I have not written the code and cannot change it but I need to use it.

FOR_EACH_CONST is:

#define FOR_EACH_CONST(type, source, item) for (type::const_iterator item=(source).begin(),
m_end_##item=(source).end(); item!=m_end_##item; ++item)
 
I

Ian Collins

I have not written the code and cannot change it but I need to use it.

FOR_EACH_CONST is:

#define FOR_EACH_CONST(type, source, item) for (type::const_iterator
item=(source).begin(), m_end_##item=(source).end(); item!=m_end_##item;
++item)

Why "typename T" rather than just T?
 
N

none

Ian said:
Why "typename T" rather than just T?

Yes I tried to remove that earlier don't think it makes any sense, but then I just get:

template <class T>
void AddSeeds(const T& points) {
FOR_EACH_CONST(T, points, i) m_Seeds.push_back(*i);
}

error: expected primary-expression before ‘,’ token

But it must have something todo with they way the macro is used.
 
I

Ian Collins

Yes I tried to remove that earlier don't think it makes any sense, but
then I just get:

template <class T>
void AddSeeds(const T& points) {
FOR_EACH_CONST(T, points, i) m_Seeds.push_back(*i);
}

error: expected primary-expression before ‘,’ token

But it must have something todo with they way the macro is used.

Your best bet is to remove the silly macro.
 
J

Jonathan Lee

I have not written the code and cannot change it but I need to use it.

Well.. it seems to work for me (g++ 4.4.3):


#include <vector>
#include <iostream>

#define FOR_EACH_CONST(type, source, item) for (type::const_iterator
item=(source).begin(), \
m_end_##item=(source).end(); item!=m_end_##item; ++item)

std::vector<int> dummy;
std::vector<int> m_Seeds;

template<class T>
void AddSeeds(const T& points) {
FOR_EACH_CONST(typename T, points, i) m_Seeds.push_back(*i);
}

int main() {
dummy.push_back(5);
AddSeeds(dummy);
std::cout << m_Seeds.back() << std::endl;
}


--Jonathan
 
N

none

Jonathan said:
Because the macro doesn't put "typename" in front of
type::const_iterator, where it's needed.

--Jonathan

good suggestion but:

#define FOR_EACH_CONST(type, source, item) for (typename type::const_iterator item=(source).begin(),
m_end_##item=(source).end(); item!=m_end_##item; ++item)

does not help :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top