Thread: headers and objects/classes

  1. #1
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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