Thread: Problem passing vector element

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    Problem passing vector element

    I have three classes.
    Trial
    Robot
    WriteToFile


    Trial is holding a pointer to a WriteToFile object and for a Robot object.

    Trial is calling Robot method to return a string and passes it to his WriteToFile.

    Robot is containing a private
    Code:
    vector<int> samplesRobotToKalmanFilter;
    I initialized it using
    Code:
    samplesRobotToKalmanFilter.clear();
    And the return method
    Code:
    const vector<int>& Robot::GetSamplesReadUsingRobot() const
    {
            if(samplesRobotToKalmanFilter.empty())
                   cout<<"empty\n";    //INDEED EMPTY
    	return samplesRobotToKalmanFilter;
    }
    Inside this method I can see that samplesRobotToKalmanFilter is empty



    WriteToFile has the following method
    Code:
    void WriteToExternalFile::WriteValuesToVector(const vector<int>& samplesvec, const vector<double>& valuesXY)
    {
         if(samplesvec.empty())
                   cout<<"empty\n";    //NOT SHOWING EMPTY
    }
    I call this last method from Trial instance:
    Code:
    writeinput->WriteValuesToVector(robot->GetSamplesReadUsingRobot() , robot->GetinputForKalmanFilter());

    The problem: inside this last method samplesvec is no longer considered empty...

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    and does the argument robot->GetSamplesReadUsingRobot() output empty?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Mario F. View Post
    and does the argument robot->GetSamplesReadUsingRobot() output empty?
    Just checked; No, it is not empty. I guess that the problem is the output of this method

    Code:
    const vector<int>& Robot::GetSamplesReadUsingRobot() const
    {
    	return samplesRobotToKalmanFilter;
    }
    Inside the method samplesRobotToKalmanFilter is empty, but passing it by reference to another vector, the new vector is not considered empty.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    How exactly is samplesRobotToKalmanFilter being initialized?

    Please summarize the constructor and where is exactly is clear() being called.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Mario F. View Post
    How exactly is samplesRobotToKalmanFilter being initialized?

    Please summarize the constructor and where is exactly is clear() being called.
    I run a clear method, right after it I call the method to return the vector and compare it to another. Then I check if the second vector is empty too.


    I figured out that the problem is something about passing the vector by reference. I don't know the solution to the problem yet.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is the Robot object that you get the values from still valid?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I figured out that the problem is something about passing the vector by reference. I don't know the solution to the problem yet.
    I doubt it. When you pass by reference, you are not creating a copy of the object. If empty() is true for the original object, it should also be true for the reference.

    What I suspect is that somewhere between you call clear() and Robot::GetSamplesReadUsingRobot() the object is being copied by value. Another option is that clear is never called.

    You can test all of this by putting a break in the line clear() is called and start stepping through the code. If your app doesn't break, you know clear() is not being called.

    EDITL: matsp also has a point
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  2. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  3. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. problem with passing data, again.
    By vnrabbit in forum C Programming
    Replies: 3
    Last Post: 10-22-2002, 02:14 PM