Thread: Problems with vectors of objects!

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Problems with vectors of objects!

    I am currently programming a digital circuit simulator program (uni coursework) ...I have created a vector of objects...

    vector <LogicGate*> theCircuit;

    Inside logic gate there are functions for all logic gates! ...then there are seperate sub-classes that are derrived from class LogicGate to over-ride some functions to give the ability to create different types of logic; and, or, xor, not. My vector can contain any of these classes, this works fine...

    Code:
    int Circuit::addGate(int inType){
    	switch(inType){
    	  case 0 : theCircuit.push_back(new InputGate(newID(), 0)); return 0;
    	  case 1 : theCircuit.push_back(new InputGate(newID(), 1)); return 0;
    	  case 2 : theCircuit.push_back(new AndGate(newID(), 2));   return 0;
    	  case 3 : theCircuit.push_back(new OrGate(newID(), 3));    return 0;
    	  case 4 : theCircuit.push_back(new XorGate(newID(), 4));   return 0;
    	  case 5 : theCircuit.push_back(new NotGate(newID(), 5));   return 0;
    	  default : return -1;
    	}
    }
    // NOTE: newID() is a small function that generates (and then returns) unique id codes.
    // To give you an idea what the values are for constructing logic gates; the constructor for the andgate class...
    
    // AndGate::AndGate(int inID, int inType){ .. sets id and logic gate type here .. }
    So now i have my vector with the above logic gate objects inserted! ...I thought therefore, that i could easily do the following...
    Code:
    for (int i=0; i<theCircuit.size(); i++){
      // is current gate an input gate?
      if (theCircuit.at(i).getType() != 0) { ...some code here... }
    }
    with getType() function being declared in class LogicGate...

    Code:
    class LogicGate{
    private:
        int gateType; // The type of logic gate
        int gateID;   // The ID for the logic gate
        int pID1;     // The first parent for the logic gate
        int pID2;     // The second parent for the logic gate
        int input1;   // The first input value for the logic gate
        int input2;   // The second input value for the logic gate
        int output;   // The output value for the logic gate
    public:
      // Class Methods:
      void setID(int);		   // sets the gate ID number
      int getType();			   // returns the gate type code
      void setType(int);		   // sets the gate type code
      int getID();               // returns gates ID
      int getPID1();             // returns gates first parent ID
      int getPID2();             // returns gates second parent ID
      void setPID1(int);         // sets ID of gates first parent
      void setPID2(int);         // sets ID of gates second parent
      void setInput1(int);       // sets gates first input value
      void setInput2(int);       // sets gates second input value
      int getInput1();           // returns value of first input
      int getInput2();           // returns value of second input
      // Virtual Methods:
      virtual int getOutput();   // returns gates output value 
    };
    However; at compilation i get the error...
    Code:
    :\WORK\CIRCUIT.CPP(100) : error C2228: left of '.getType' must have class/struct/union type
    Its confusing, because to me it appears that this should work fine!!! ...I tried a simpler version using vectors of objects and that worked fine, i could access all the objects functions using the vector.at(x).functionName() with no problems at all!!! so what is going on here??? ...anyone know what i am doing wrong???

    Cheers!!! Matt..

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your vector contains pointers to logic gates
    use
    Code:
    vector.at(x)->functionName();
    Kurt

  3. #3
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    DOH!!!! ...I had even put that in my little tester program!!!! arrrggghhh!!! I swear this programming nonsense is making me insane in the membrain!!! Cheers man!!!! such little problems!!!!! arrrggghhhh!!!!!

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bobthebullet990
    DOH!!!! ...I had even put that in my little tester program!!!! arrrggghhh!!! I swear this programming nonsense is making me insane in the membrain!!! Cheers man!!!! such little problems!!!!! arrrggghhhh!!!!!
    You shure should stop trying to program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Vectors of Vectors
    By Beamu in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2008, 05:23 AM
  2. first opengl game, problems.
    By n3v in forum Game Programming
    Replies: 1
    Last Post: 07-11-2006, 08:22 PM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM