Thread: I need help. please

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    I need help. please

    Hi !

    I try to save a shapes from a vector to a file. When I compile a code I get a very large error that begin:
    "C:/Qt2009_02/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stream_iterator.h: In member fu
    nction `std:stream_iterator<_Tp, _CharT, _Traits>& std:stream_iterator<_Tp, _CharT, _Traits>:perator=(const _Tp&) [wi
    th _Tp = const ShapePtr, _CharT = char, _Traits = std::char_traits<char>]':" ...

    Here is Main (NewShapes.cpp):
    Code:
    #include <vector>
    #include <fstream>
    #include <iterator>
    #include <string>
    #include "Vertex.h"
    #include "ShapePtr.cpp"
    using namespace std;
    
    main() {
      vector<ShapePtr> shapevec;
      Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
      shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
      shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );
      shapevec.push_back( ShapePtr(new Rectangle(4, 10, 2, 4)) );
      shapevec.push_back( ShapePtr(new Point(6, 7, 1)) );
    
      ofstream os("fil.dat");
      ostream_iterator<const ShapePtr> shapeout(os, "\n");
      copy(shapevec.begin(), shapevec.end(), shapeout);
      os.close();
    }
    What I'm missing, I wander.
    Thank You in advance.

    Adalte.

    Pd: I attached the files with code (NewShapes.cpp, ShapePtr.h, ShapePtr.cpp, Vertex.h) and full error message.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First of all, fix these things.
    When people point out problems, you fix them instead of ignoring them.

    Implicit main is invalid in C++.
    main() {
    should be
    int main() {

    And secondly, do not include .cpp files.

    As for the error, it seems to be related to the copy-line. I would suspect that the ostream_iterator for your file doesn't support the necessary interface for std::copy.
    In other words, you cannot use it with std::copy.
    Last edited by Elysia; 07-12-2009 at 04:00 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Help, please
    You need to start coming up with better topic titles as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You know a simple googling of a file/vector tutorial might have helped you out a lot more.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Yes, Elysia You are right:
    "it seems to be related to the copy-line." I tested it: I remarked it and didn't get errors.

    "I would suspect that the ostream_iterator for your file doesn't support the necessary interface for std::copy." The question is:

    What I can do to get the ostream_iterator for file to support the necessary interface for std::copy ?

    Main:
    Code:
    #include <vector>
    #include <fstream>
    #include <iterator>
    #include <algorithm>
    #include <string>
    #include "Vertex.h"
    #include "Shapes.h"
    using namespace std;
    
    int main() {
      vector<ShapePtr> shapevec;
      Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
      shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
      shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );
      shapevec.push_back( ShapePtr(new Rectangle(4, 10, 2, 4)) );
      shapevec.push_back( ShapePtr(new Point(6, 7, 1)) );
    
      ofstream os("fildat.txt");
      ostream_iterator<const ShapePtr> shapeout(os, "\n");
      copy(shapevec.begin(), shapevec.end(), shapeout);
      os.close();
      ...
    }
    Thank You in advanced.

    Adalte.
    Last edited by Adalte; 07-13-2009 at 05:50 PM.

Popular pages Recent additions subscribe to a feed