'Ello. I'm having a concern about the ability for several objects that require access to one another. Visual studio intellisence shows no errors, but the compiler does. Mostly that classes become unrecognized. I can get different errors by shuffling around the classes within the sapa namespace. Sorry, its a bit long.

#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>

using namespace std;

#define DEBUG true
/*
SAPA developmental classes and process
to demonstrate class and functioning 
/thread conjunctions

*/

//definitions
typedef unsigned int UINT;
enum{second, minute, hour};
#define EXITSAPA        100
#define SECOND          200
#define MINUTE          201
#define HOUR            202
#define MakeNewThread   300
namespace sapa{


    struct SubPacket{
        UINT signal;
         vector <string> target;
         vector <string> t_data;
         vector <double> data;
    };
    struct Base_thread_data{//for hardcode handles
        int m_id;
    };
    struct thread_data{//for enumerated handles
        int m_id;
        thread_data(int id) : m_id(id){}
    };
        namespace SystemFunctions{
        void SAPAEXIT(Packet input){
            input.sys->quit=true;

        }
        void NewThread(Packet input){//create new thread and run instructions
            //information on occurence
            UINT TimeTemp=0;
            UINT Period=0;//time needed to elpse in order to run
            while (!input.sys->quit){


            }
        }
        void UpdataTimeSeconds(Packet input){
            input.sys->TimeElapsed++;
        }
    }
class ConsoleIO{
    string raw;

    vector <string> Vraw;

    vector <UINT>    signal;
    vector <string> Target;
    vector <double> data;
    vector <string> t_data;


public:
    void ConsoleIn(string input){
        raw=input;


    }
    Packet PushDemoPacket(){
        SubPacket demo;
        demo.signal=EXITSAPA;
    }

    vector <SubPacket> packetqeue;
    };
class Clock{
public:
    vector <SubPacket> qeue;
    void MakeSubPacket(unsigned int idata){
        SubPacket sp;
        sp.signal=idata;
        qeue.push_back(sp);
    }
    Clock(){
        sec=0;
        min=0;
        hr=0;
    }
    int sec,min,hr;
    void UpdateSec(){//updates timecode
        sec++;
        MakeSubPacket(second);
        if (sec>=60){
            sec=0;
            min++;
            MakeSubPacket(min);
            if (min>=60){
                min=0;
                hr++;
                MakeSubPacket(hr);
            }
        }
    }//update time code
    string get_timecode(){
        string hold;
        hold=sec+':'+min+':'+hr;
        return hold;
        }
    };
class System{//holds information for program operation
public:
    bool quit;
    System(){
        quit=false;
        TimeElapsed=0;
    }

    UINT TimeElapsed;//seconds passed since program run

    int Threads;//number of user threads running
    vector <int> ThreadIDs;//holds the ids of all running threads so none are duplicated and old exited ids can be recycled
    };
class messenger{
public:
    System * sys;
    Clock * clk;
    ConsoleIO*Cio;
    bool SystemLink;
    bool ClockLink;
    bool ConsoleLink;
public:




    messenger(){
        SystemLink=false;
        ClockLink=false;
        ConsoleLink=false;

        Threads=0;
    }
    void setSystem(System*psys){
        sys=psys;
        delete psys;
        psys=NULL;
        SystemLink=true;
    }
    void setClock(Clock*pclk){
        clk=pclk;
        delete pclk;
        pclk=NULL;
        ClockLink=true;
    }
    void setConsoleIO(ConsoleIO*CIO){
        Cio=CIO;
        delete CIO;
        CIO=NULL;
        ConsoleLink=true;
    }

    void ProcessPacket(Packet data){//process information
        /*
        PROCESSING METHOD

        switch(signal) to identify correct function
        pass data packet to function (implied)
        Yeaah!
        */

    }
    void ProcessSubPacket(SubPacket sdata){//convert subpacet to packet and process
        Packet data;
        data.sys=sys;
        data.data=sdata.data;
        data.signal=sdata.signal;
        data.target=sdata.target;
        data.t_data=sdata.t_data;
        ProcessPacket(data);
    }


};

struct Packet{
        System*sys;
         UINT signal;
         vector <string> target;
         vector <string> t_data;
         vector <double> data;
         Packet():sys(){sys=NULL;}
    };

static DWORD WINAPI ClockThread(LPVOID lpParameter){//runs messenger thread with handle
    Clock*clk = (Clock*)lpParameter;
    //does nothing more than tick signals and qeue them
    cout<<"Clock Thread Launched\n";
    while (1!=2){
        Sleep(1000);
        clk->UpdateSec();
    }
    return 0;
}

static DWORD WINAPI MessengerThread(LPVOID lpParameter){//runs messenger thread with handle
    messenger*msg = (messenger*)lpParameter;
    cout<<"Messenger Thread Launched\n";

    for (UINT objloop=0;!msg->sys->quit;objloop++){//process and delete accumulated information
        if (msg->Cio->packetqeue.size()>0){msg->ProcessSubPacket(msg->Cio->packetqeue[0]);//process
        msg->Cio->packetqeue.erase(msg->Cio->packetqeue.begin());//delete packet from qeue
        }
        if (msg->clk->qeue.size()>0){     msg->ProcessSubPacket(msg->clk->qeue[0]);
        msg->clk->qeue.erase(msg->clk->qeue.begin());
        }
        objloop=0;//
    }
    return 0;
}
void StartMessenger(messenger*msg){
    CreateThread(NULL,0,MessengerThread, msg,0,0);
    delete msg;
    msg=NULL;
}
void StartClock(Clock*clk){
    CreateThread(NULL,0,ClockThread, clk,0,0);
    delete clk;
    clk=NULL;
}
};

using namespace sapa;
void main(){
    //create primary system objects
    System sys;
    Clock clk;
    messenger msg;
    ConsoleIO Cio;
    //Give messenger access to data
    msg.setClock(&clk);
    msg.setConsoleIO(&Cio);
    msg.setSystem(&sys);
    //starts running independant system threads threads
    StartMessenger(&msg);
    StartClock(&clk);

    cout<<"SAPA developmental build:\n";

    string usin;

    while (!sys.quit){
        getline(cin,usin);
        Cio.ConsoleIn(usin);//insert into Cio

    }
    cout<<endl<<endl;
    cout<<"Closing process threads\n\n";
    system("pause");

}

About the program: It runs on 3 threads: A command driven thread, a timer thread, and a continous thread. The command driven thread accepts a user input, breaks down the input into several catergorized vectors (not shown in this sample) and then puts them into a 'subpacket' class stored in a vector qeue. The timer thread makes a 'subpacket' for every second, minute, hour, and day that passes, also adding to a qeue. The messenger class runs continously. It has a pointer to the timer and command classes. Its supposed to take an item from the qeue, add in the pointer to the 'system' class, and pass it off to a function.

I've tried all sorts of different methods, but i just can't seem to get the kind of memory access i want. Its supposed to eventually all go into a static library and act almost like managed code. Im almost thinking Java might be better suited for this. Think anyone can help?

Recommended Answers

All 3 Replies

Please post the errors the compiler gives with the above code.

It's all down to the order in which things were being declared and used. Some things were being used in the code before they had been declared.

The following compiles, but crashes at runtime. I'll leave you to debug it!
All I did was move the declarations for the System class and the Packet struct up a bit!:

#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>

using namespace std;

#define DEBUG true
/*
SAPA developmental classes and process
to demonstrate class and functioning 
/thread conjunctions

*/

//definitions
typedef unsigned int UINT;
enum{second, minute, hour};
#define EXITSAPA        100
#define SECOND          200
#define MINUTE          201
#define HOUR            202
#define MakeNewThread   300
namespace sapa{


    struct SubPacket{
        UINT signal;
        vector <string> target;
        vector <string> t_data;
        vector <double> data;
    };
    struct Base_thread_data{//for hardcode handles
        int m_id;
    };
    struct thread_data{//for enumerated handles
        int m_id;
        thread_data(int id) : m_id(id){}
    };

    class System{//holds information for program operation
    public:
        bool quit;
        System(){
            quit=false;
            TimeElapsed=0;
        }

        UINT TimeElapsed;//seconds passed since program run

        int Threads;//number of user threads running
        vector <int> ThreadIDs;//holds the ids of all running threads so none are duplicated and old exited ids can be recycled
    };

    struct Packet{
        System*sys;
        UINT signal;
        vector <string> target;
        vector <string> t_data;
        vector <double> data;
        Packet():sys(){sys=NULL;}
    };

    namespace SystemFunctions{
        void SAPAEXIT(Packet input){
            input.sys->quit=true;

        }
        void NewThread(Packet input){//create new thread and run instructions
            //information on occurence
            UINT TimeTemp=0;
            UINT Period=0;//time needed to elpse in order to run
            while (!input.sys->quit){


            }
        }
        void UpdataTimeSeconds(Packet input){
            input.sys->TimeElapsed++;
        }
    }
    class ConsoleIO{
        string raw;

        vector <string> Vraw;

        vector <UINT>    signal;
        vector <string> Target;
        vector <double> data;
        vector <string> t_data;


    public:
        void ConsoleIn(string input){
            raw=input;


        }
        Packet PushDemoPacket(){
            SubPacket demo;
            demo.signal=EXITSAPA;
        }

        vector <SubPacket> packetqeue;
    };
    class Clock{
    public:
        vector <SubPacket> qeue;
        void MakeSubPacket(unsigned int idata){
            SubPacket sp;
            sp.signal=idata;
            qeue.push_back(sp);
        }
        Clock(){
            sec=0;
            min=0;
            hr=0;
        }
        int sec,min,hr;
        void UpdateSec(){//updates timecode
            sec++;
            MakeSubPacket(second);
            if (sec>=60){
                sec=0;
                min++;
                MakeSubPacket(min);
                if (min>=60){
                    min=0;
                    hr++;
                    MakeSubPacket(hr);
                }
            }
        }//update time code
        string get_timecode(){
            string hold;
            hold=sec+':'+min+':'+hr;
            return hold;
        }
    };

    class messenger{
    public:
        System * sys;
        Clock * clk;
        ConsoleIO*Cio;
        bool SystemLink;
        bool ClockLink;
        bool ConsoleLink;
    public:

        messenger(){
            SystemLink=false;
            ClockLink=false;
            ConsoleLink=false;

            //Threads=0; \\ <<<< What is this!
        }
        void setSystem(System*psys){
            sys=psys;
            delete psys;
            psys=NULL;
            SystemLink=true;
        }
        void setClock(Clock*pclk){
            clk=pclk;
            delete pclk;
            pclk=NULL;
            ClockLink=true;
        }
        void setConsoleIO(ConsoleIO*CIO){
            Cio=CIO;
            delete CIO;
            CIO=NULL;
            ConsoleLink=true;
        }

        void ProcessPacket(Packet data){//process information
            /*
            PROCESSING METHOD

            switch(signal) to identify correct function
            pass data packet to function (implied)
            Yeaah!
            */

        }
        void ProcessSubPacket(SubPacket sdata){//convert subpacet to packet and process
            Packet data;
            data.sys=sys;
            data.data=sdata.data;
            data.signal=sdata.signal;
            data.target=sdata.target;
            data.t_data=sdata.t_data;
            ProcessPacket(data);
        }


    };

    static DWORD WINAPI ClockThread(LPVOID lpParameter){//runs messenger thread with handle
        Clock*clk = (Clock*)lpParameter;
        //does nothing more than tick signals and qeue them
        cout<<"Clock Thread Launched\n";
        while (1!=2){
            Sleep(1000);
            clk->UpdateSec();
        }
        return 0;
    }

    static DWORD WINAPI MessengerThread(LPVOID lpParameter){//runs messenger thread with handle
        messenger*msg = (messenger*)lpParameter;
        cout<<"Messenger Thread Launched\n";

        for (UINT objloop=0;!msg->sys->quit;objloop++){//process and delete accumulated information
            if (msg->Cio->packetqeue.size()>0){msg->ProcessSubPacket(msg->Cio->packetqeue[0]);//process
            msg->Cio->packetqeue.erase(msg->Cio->packetqeue.begin());//delete packet from qeue
            }
            if (msg->clk->qeue.size()>0){     msg->ProcessSubPacket(msg->clk->qeue[0]);
            msg->clk->qeue.erase(msg->clk->qeue.begin());
            }
            objloop=0;//
        }
        return 0;
    }
    void StartMessenger(messenger*msg){
        CreateThread(NULL,0,MessengerThread, msg,0,0);
        delete msg;
        msg=NULL;
    }
    void StartClock(Clock*clk){
        CreateThread(NULL,0,ClockThread, clk,0,0);
        delete clk;
        clk=NULL;
    }
};

using namespace sapa;
void main(){
    //create primary system objects
    System sys;
    Clock clk;
    messenger msg;
    ConsoleIO Cio;
    //Give messenger access to data
    msg.setClock(&clk);
    msg.setConsoleIO(&Cio);
    msg.setSystem(&sys);
    //starts running independant system threads threads
    StartMessenger(&msg);
    StartClock(&clk);

    cout<<"SAPA developmental build:\n";

    string usin;

    while (!sys.quit){
        getline(cin,usin);
        Cio.ConsoleIn(usin);//insert into Cio

    }
    cout<<endl<<endl;
    cout<<"Closing process threads\n\n";
    system("pause");

}

One thing that does look highly suspicious is that you are deleting pointers to objects that are on the stack... Which is definitely wrong. You aren't using new anywhere in your program and if you didn't use new to allocate the memory, then you shouldn't be attempting to delete it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.