Problem passing vector element

This is a discussion on Problem passing vector element within the C++ Programming forums, part of the General Programming Boards category; I have three classes. Trial Robot WriteToFile Trial is holding a pointer to a WriteToFile object and for a Robot ...

  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
    Portugal
    Posts
    7,581
    and does the argument robot->GetSamplesReadUsingRobot() output empty?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,581
    How exactly is samplesRobotToKalmanFilter being initialized?

    Please summarize the constructor and where is exactly is clear() being called.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,581
    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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21