typedef problem

A

aaragon

Hello everyone,

I'm trying to run some simple code but for some reason it doesn't work
and I've been staring at it for a long time without a single clue of
what's going on. This is what happens, I have a simple class in a file
named "geom.h". This class has a very simple type definition, as
follows:

// type def for domainGraph
typedef ...... domainGraph;

// Domain class
class Domain
{
// private variables
...

public:

typedef graph_traits<domainGraph>::vertex_iterator vertex_iter;

// constructors
...
// member functions
...
};

Now, I try to instantiate an object of vertex_iter from the
constructor of another class, in file "graph.h" but when I write the
following code within the constructor:

// code for constructor in "graph.h"
....
typedef Domain::vertex_iter viter;
viter vi,vi_end;
....

I have the following compiler message:
graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: 'viter' was not declared in this scope
graph.h:xxx: error: expected `;' before 'vi'
make: *** [gui.o] Error 1

The funny thing is that this works from files other than "graph.h". I
tried to do the same form main.cxx and it works fine. It's as if the
Domain class is not seen from the "graph.h" file. Anyone has any clue
of what is happening here???
Thanks for your help.

a^2
 
S

Salt_Peter

Hello everyone,

I'm trying to run some simple code but for some reason it doesn't work
and I've been staring at it for a long time without a single clue of
what's going on. This is what happens, I have a simple class in a file
named "geom.h". This class has a very simple type definition, as
follows:

// type def for domainGraph
typedef ...... domainGraph;

// Domain class
class Domain
{
// private variables
...

public:

typedef graph_traits<domainGraph>::vertex_iterator vertex_iter;

// constructors
...
// member functions
...

};

Now, I try to instantiate an object of vertex_iter from the
constructor of another class, in file "graph.h" but when I write the
following code within the constructor:

// code for constructor in "graph.h"
...
typedef Domain::vertex_iter viter;

typedef typename Domain::vertex_iter VIter;
viter vi,vi_end;
...

I have the following compiler message:
graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: 'viter' was not declared in this scope
graph.h:xxx: error: expected `;' before 'vi'
make: *** [gui.o] Error 1

The funny thing is that this works from files other than "graph.h". I
tried to do the same form main.cxx and it works fine. It's as if the
Domain class is not seen from the "graph.h" file. Anyone has any clue
of what is happening here???
Thanks for your help.

a^2
 
A

aaragon

Hello everyone,
I'm trying to run some simple code but for some reason it doesn't work
and I've been staring at it for a long time without a single clue of
what's going on. This is what happens, I have a simple class in a file
named "geom.h". This class has a very simple type definition, as
follows:
// type def for domainGraph
typedef ...... domainGraph;
// Domain class
class Domain
{
// private variables
...

typedef graph_traits<domainGraph>::vertex_iterator vertex_iter;
// constructors
...
// member functions
...

Now, I try to instantiate an object of vertex_iter from the
constructor of another class, in file "graph.h" but when I write the
following code within the constructor:
// code for constructor in "graph.h"
...
typedef Domain::vertex_iter viter;

typedef typename Domain::vertex_iter VIter;
viter vi,vi_end;
...
I have the following compiler message:
graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: 'viter' was not declared in this scope
graph.h:xxx: error: expected `;' before 'vi'
make: *** [gui.o] Error 1
The funny thing is that this works from files other than "graph.h". I
tried to do the same form main.cxx and it works fine. It's as if the
Domain class is not seen from the "graph.h" file. Anyone has any clue
of what is happening here???
Thanks for your help.

I tried that too but I have the same error:

graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: invalid type in declaration before ';' token
make: *** [gui.o] Error 1
 
S

Salt_Peter

typedef typename Domain::vertex_iter VIter;
viter vi,vi_end;
...
I have the following compiler message:
graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: 'viter' was not declared in this scope
graph.h:xxx: error: expected `;' before 'vi'
make: *** [gui.o] Error 1
The funny thing is that this works from files other than "graph.h". I
tried to do the same form main.cxx and it works fine. It's as if the
Domain class is not seen from the "graph.h" file. Anyone has any clue
of what is happening here???
Thanks for your help.
a^2

I tried that too but I have the same error:

graph.h: In constructor 'mVAC::mVAC(Domain&, Chromosome&)':
graph.h:xxx: error: 'vertex_iter' in class 'Domain' does not name a
type
graph.h:xxx: error: invalid type in declaration before ';' token
make: *** [gui.o] Error 1


That should be a clue as to what you need to do - vertex_iter is a
dependant type.
The error is quite clear.
I'm not about to start guessing and assuming as to whay your code
looks like.
Specially since you are attempting to initialize an instance of some
iterator in a class other than the one it lives in. That requires
special considerations depending on the container involved
(std::vector is dynamic and so an iterator will be invalidated if the
vector resizes past its reserve).

Perhaps you can try asking your question with compileable code that
recreates the issue.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

template < typename N >
class Domain
{
std::vector< N > v;
public:

// ctors
Domain() : v() { }
Domain(const Domain& copy)
{
v = copy.v;
}
// iteration
typedef typename std::vector< N >::iterator iterator;
typedef typename std::vector< N >::const_iterator const_iterator;
const_iterator begin() const
{
return v.begin();
}
const_iterator end() const
{
return v.end();
}
// member functions
void push_back(const N& r_n)
{
v.push_back(r_n);
}
};

template< typename N >
class Graph
{
Domain< N > domain;
public:
// ctors
Graph() : domain() { }
// member functions
void push_back(const N& r_n)
{
domain.push_back(r_n);
}
void display() const
{
typedef typename Domain< N >::const_iterator DIter;
for( DIter diter = domain.begin();
diter != domain.end();
++diter )
{
std::cout << *diter;
std::cout << "\n";
}
}
};

int main(int argc, char* argv[])
{
Graph< int > graph;
graph.push_back(0);
graph.push_back(1);
graph.push_back(2);

graph.display();
}

/*
0
1
2
*/
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top