typedef problem

Discussion in 'C++' started by aaragon, Apr 12, 2007.

  1. aaragon

    aaragon Guest

    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
     
    aaragon, Apr 12, 2007
    #1
    1. Advertising

  2. aaragon

    Salt_Peter Guest

    On Apr 11, 11:24 pm, "aaragon" <> wrote:
    > 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
     
    Salt_Peter, Apr 12, 2007
    #2
    1. Advertising

  3. aaragon

    aaragon Guest

    On Apr 12, 1:29 am, "Salt_Peter" <> wrote:
    > On Apr 11, 11:24 pm, "aaragon" <> wrote:
    >
    >
    >
    > > 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


    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
     
    aaragon, Apr 12, 2007
    #3
  4. aaragon

    Salt_Peter Guest

    On Apr 12, 11:06 am, "aaragon" <> wrote:
    > On Apr 12, 1:29 am, "Salt_Peter" <> wrote:
    >
    >
    >
    > > On Apr 11, 11:24 pm, "aaragon" <> wrote:

    >
    > > > 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

    >
    > 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
    */
     
    Salt_Peter, Apr 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Borghgraef

    Friend class and typedef problem

    Alex Borghgraef, Feb 11, 2004, in forum: C++
    Replies:
    1
    Views:
    3,349
    John Carson
    Feb 12, 2004
  2. marco
    Replies:
    2
    Views:
    364
    marco
    Sep 2, 2004
  3. Takeshi
    Replies:
    4
    Views:
    5,436
    Takeshi
    Oct 25, 2004
  4. Fao
    Replies:
    6
    Views:
    565
    gooch
    Jan 27, 2005
  5. oor
    Replies:
    0
    Views:
    1,394
Loading...

Share This Page