I am getting an error in Xcode when using templates in C++. Can someone tell me what is wrong?
The first version reports an error in Xcode, but not in Visual Studio.
// Version 1: Error in Xcode, but not Visual Studio
template<typename LengthT, typename VertexT>
int MyGraphAlgorithm(...arguments omitted...)
{
using namespace boost;
typedef property<vertex_distance_t, LengthT> VertextProperties_t;
typedef adjacency_list<vecS, vecS, directedS, VertextProperties_t> Graph;
// In next line Xcode reports: "error: expected `;' before 'vertexInitial'"
graph_traits<Graph>::vertex_descriptor vertexInitial(100);
}
The second has no error. The difference is the use of the template parameter LengthT in a templated typedef.
// Version 2: No error in Xcode or Visual Studio
template<typename LengthT, typename VertexT>
int MyGraphAlgorithm(...arguments omitted...)
{
using namespace boost;
// In the following line, LengthT has been changed to int
typedef property<vertex_distance_t, int> VertextProperties_t;
typedef adjacency_list<vecS, vecS, directedS, VertextProperties_t> Graph;
graph_traits<Graph>::vertex_descriptor vertexInitial(100);
}
The reason for the error is that the compiler has no clue what
graph_traits<Graph>::vertex_descriptoris. Is it a static member or a type? If it’s a type then you must say so:The reason the compiler isn’t smart enough to figure it out on its own is because
LengthTis a template parameter. It can be anything, and so at the time of template declaration the compiler can’t tell what its value is going to be, and the typedef is thus ambiguous.