Copy Problems.

This is a discussion on Copy Problems. within the C++ Programming forums, part of the General Programming Boards category; The following code compiles clean, and executes without error, however when the objects are printed out, they appear to be ...

  1. #1
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    Copy Problems.

    The following code compiles clean, and executes without error, however when the objects are printed out, they appear to be empty. The datafile is just a big list of numbers, nothing fancy. Im looking for somebody to tell me what im doing wrong, how to fix it, and if there is a faster way than copy to fill my vector.

    Code:
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Bob
    {
    	public:
    		int a;
    		int b;
    		int c;
    		int d;
    		int e;
    		int f;
    		Bob(void);
    		Bob(int,int,int,int,int,int);
    		~Bob(void);
    		friend std::ostream operator << (std::ostream, Bob);
    		friend std::istream operator >> (std::istream, Bob);
    };
    
    Bob::Bob()
    {
    	a=0;
    	b=0;
    	c=0;
    	d=0;
    	e=0;
    	f=0;
    }
    
    Bob::~Bob()
    {
    }
    
    Bob::Bob(int q, int w, int s, int r, int t, int y)
    {
    	a = q;
    	b = w;
    	c = s;
    	d = r;
    	e = t;
    	f = y;
    }
    
    std::ostream operator << (std::ostream mystream, const Bob theobject)
    {
    	mystream << theobject.a << " " << theobject.b << " " << theobject.c << " "
    			 << theobject.d << " " << theobject.e << " " << theobject.f;
    	mystream << std::endl;
    	return mystream;
    }
    
    std::istream operator >> (std::istream mystream, Bob theobject)
    {
    	mystream >> theobject.a;
    	mystream >> theobject.b;
    	mystream >> theobject.c;
    	mystream >> theobject.d;
    	mystream >> theobject.e;
    	mystream >> theobject.f;
    	return mystream;
    }
    
    
    void main()
    {
    
    	string filename = "test.txt";
    	ifstream datafile;
    	datafile.open(filename.c_str());
    
    	vector<Bob> Bobs;
    	
    	copy( ( istream_iterator<Bob> (datafile) ),istream_iterator<Bob>(),back_inserter(Bobs));
    
    	cout << Bobs.size() << " Records Read!" << endl << endl;
    	cout << "The first bob contains: " << Bobs[0] << endl;
    	cout << "The next  bob contains: " << Bobs[1] << endl;
    	cout << "The next  bob contains: " << Bobs[2] << endl;
    }

  2. #2
    ___
    Join Date
    Jun 2003
    Posts
    806
    Code:
    friend std:: ostream operator
    you are using using namespace std so why do you do the std:: thing? Is it something more advanced?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    By empty, do you mean not printing out anything, or printing out garbage for those variables?

    As a side note, why not make the 6 ints a vector?

    >> Is it something more advanced?

    Nope... Relics from a previous rendition of the program would be my guess.
    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
    ___
    Join Date
    Jun 2003
    Posts
    806
    >> Is it something more advanced?

    <<Nope... Relics from a previous rendition of the program would be my guess. [/B][/QUOTE]



    What do you mean?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    empty as in the default constructor executed and they are 0's. as for the vector of ints, this entire program is nothing more than an example generated to demonstrate my problem. as for the std:: i got lazy halfway through. im just so used to typing it and not using namespace std that i still do it half the time.

  6. #6
    ___
    Join Date
    Jun 2003
    Posts
    806
    Would that create problems using both or no?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  7. #7
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    no, it causes no problems. its fair to use "using namespace std", or simply stick a std:: in front of everything, or a combonation of both. i have never had it cause problems.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Would that create problems using both or no?

    Nope.

    As for the problem, try passing the streams by reference (and then returning the reference), since you are modifying the object (stream) in the overloaded operators.
    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.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Shouldn't your variables be in private or protected instead of public?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    JaWiB - nope. note that this is nothing more than an example to demonstrate a problem im having. much simpler to take 5 minutes and crank out lazy code to show the problem than post 2000 lines and ask people to try and understand it.

    Zach L. - i just tested the code (by spitting out values during the copy) and it seems the stream isnt the problem, but the Bob object is the problem during the copy. inside the copy it contains the correct values, however outside the copy it contains only zeros. im not sure how to fix this because if i try and pass the object by reference it complains saying it will only pass the object Bob and not *Bob. ideas?

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Wait, are you passing by pointer? Pass by reference:

    ostream & operator<<(ostream & mystream, const Bob &theObject)

    A Bob& is not (to the language) the same as a Bob*.

    That should work, but unless you have another copy constructor that doesn't copy construct correctly, the other way should have worked (it would just be inefficient).

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try this:

    Remove these two lines:
    Code:
    friend std::ostream& operator << (std::ostream&, Bob&);
    friend std::istream& operator >> (std::istream&, Bob&);
    OR make them like so:
    Code:
    friend std::ostream& operator << (std::ostream&, const Bob&);
    friend std::istream& operator >> (std::istream&, Bob&);
    Change these:
    Code:
    std::ostream& operator << (std::ostream& mystream, const Bob &theobject)
    
    std::istream& operator >> (std::istream& mystream, Bob &theobject)
    (note the & usage)

    [edit]
    Added replacement for friend functions
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor question
    By BigFish21 in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2008, 12:18 PM
  2. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 03:54 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  4. Replies: 9
    Last Post: 05-19-2006, 05:19 PM
  5. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM

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