headers and objects/classes

This is a discussion on headers and objects/classes within the C++ Programming forums, part of the General Programming Boards category; Hello Im having headaches because I cant figure out what am I doing wrong. Heres the code: session.h: Code: #include ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    headers and objects/classes

    Hello

    Im having headaches because I cant figure out what am I doing wrong.

    Heres the code:


    session.h:

    Code:
    #include "service.h"
    #include "car.h"
    #include "driver.h"
    
    class session {
    public:
    	session();
    	car const &car() const { return m_car; }
    private:
    	service m_service;
    	car m_car;
    	
    	driver m_driver;
    };
    session.cpp:

    Code:
    #include "session.h"
    session::session() : m_service(), m_car(m_service), m_driver(*this) {
    }
    car.h:

    Code:
    #include "service.h"
    class car {
    public:
    	car(service &s) : m_service(s)  {
    	}
    
    	template <typename Handler>
    	void somefunc(Handler t) {
    	}
    	
    private:
    	service &m_service;
    };

    driver.h:

    Code:
    #include "session.h"
    class driver {
    public:
    	driver(session &s);
    	
    	do_stuff();
    private:
    	session m_session;
    };
    driver.cpp:

    Code:
    #include "driver.h"
    #include "session.h"
    #include "car.h"
    
    driver::driver(session &s) : m_session(s) {
    	do_stuff();
    }
    
    void driver::do_stuff() {
    	m_session.car().somefunc(someclass);
    }

    The problem is that car in session class stays 'unknown'.
    The error I get:
    error C2146: syntax error : missing ';' before identifier 'm_car' .. etc.

    How to solve this since I cannot make separated .cpp file for class car because its templated?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    What exactly is the point of the session class? It seems to me that you can just provide the driver to the car, or vice versa. At the moment it looks like you are storing driver in session, and storing session in driver. That is... weird.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I renamed the classes. The point of the session class is that it stores data/objects for classes that are under session (multithreaded environment).

    In other words..

    Driver is being controlled by session class, and driver(s) need access to session class to read data and to call functions in car class (m_car).

    The design seems good to me..

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21