Thread: problem: cant acess class methods using vectors

  1. #1
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18

    problem: cant acess class methods using vectors

    now i have a class Schedule.
    Code:
    class Schedule
    {
    public:
    	Schedule(int &dailyCounter)
    {	cout<<"In Daily ctor"<<endl;
    	dailyCounter++;
    };
    	~Schedule(){cout<<"in daily dtor"<<endl;};
    	void printSchedule()
                     {cout<<"PrintSchedule"<<endl;};
    private:
    	int dailyCounter;
    
    };
    in main i do...
    Code:
      int i=0;
      static std::vector <Schedule> newdaily;
    for(i=0;i<10;i++)
    {  Schedule tempDaily = Schedule::Schedule(i);
      newdaily.push_back(tempDaily);
      newdaily[i].printSchedule();
    }
    it gives me error at this line in main
    it gives that object couldnt get created
    newdaily[dailyCounter].printSchedule;
    why i cant acess a method of Schedule using vector indexing...

  2. #2
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    ignore this line at the end
    newdaily[dailyCounter].printSchedule;

    and cosider this correct
    newdaily[i].printSchedule();

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    This line is rather suspect:
    Code:
    Schedule tempDaily = Schedule::Schedule(i);
    The constructor isn't static, so call it like this:
    Code:
    Schedule tempDaily(i);
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    that program compiles fine on my compiler, borland 6.
    Be a leader and not a follower.

  5. #5
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    ok i tried this and thsi isnt working too
    neither in VC++ nor on Solaris GNU compiler
    Code:
    class Schedule
    {
    public:
    	Schedule()
                       {cout<<"In Daily ctor"<<endl;};
    	~Schedule(){cout<<"in daily dtor"<<endl;};
    	void printSchedule()
                     {cout<<"PrintSchedule"<<endl;};
    private:
    
    };
    --------------------------------------------------------------------------------

    in main i do...

    Code:
      int i=0;
      static std::vector <Schedule> newdaily;
    for(i=0;i<10;i++)
    {  Schedule tempDaily();
      newdaily.push_back(tempDaily);
      newdaily[i].printSchedule();
    }
    but still the same error

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    well here is an exact code extract from what i did, it works fine, can't understand why you are having that problem.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Schedule
    {
      public:
    	Schedule(int &dailyCounter)
            {
              cout<<"In Daily ctor"<<endl;
    	  dailyCounter++;
            };
    	~Schedule(){cout<<"in daily dtor"<<endl;};
    	void printSchedule(){cout<<"PrintSchedule"<<endl;};
    private:
    	int dailyCounter;
    };
    
    
    int main(void)
    {
      int i=0;
      static std::vector <Schedule> newdaily;
      for(i=0;i<10;i++)
      {
        Schedule tempDaily(i);
        newdaily.push_back(tempDaily);
        newdaily[i].printSchedule();
      }
      return 0;
    }
    Be a leader and not a follower.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    Schedule tempDaily( );
    Give it the parameter:
    Code:
    Schedule tempDaily(i);
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    yeah it works fine with subdene solution.... i actually had another bug in my original complete program
    thanx all ppl

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Once you get it to compile, you should notice that the rest of your original code doesn't do what you want it to do. You can try to figure out what I'm talking about on your own, but a hint is to look at your constructor incrementing dailyCounter. Which dailyCounter variable is it incrementing? If that is what you meant to do, then what are the effects when the incremented variable is used later?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Problem with class member layout (footprint)
    By Osaou in forum C++ Programming
    Replies: 3
    Last Post: 07-24-2006, 11:33 AM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM