Thread: Help, please

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

    Help, please

    Hi !
    When I compile this piece of code I get the following error:
    "NewShapes.cpp: In function `int main()':
    NewShapes.cpp:12: error: `ShapePtr' cannot appear in a constant-expression
    NewShapes.cpp:12: error: template argument 1 is invalid
    NewShapes.cpp:12: error: template argument 2 is invalid"

    If necesary, I may change all code but not the code in main (sorry). I may add code to the main but not change it.

    Here is NewShapes.cpp (main):
    Code:
    #include <vector>
    #include <fstream>
    #include <string>
    #include "Vertex.h"
    #include "Shape.cpp"
    #include "Listan.h"
    using namespace std;
    
    main() {
      cout << endl;
      Listan ShapePtr;
      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 is("fil.dat");
      ostream_iterator<const shapePtr> shapeout(os, "\n");
      copy( shapevec.begin(), sahpevec.end(), shapeout);
      os.close();
    
      ifstream is("fil.dat");
      for (list<shapePtr>::iterator it = shapelist.begin(); it != shapelist.end(); it++)
          cout << *it << endl;
      shapevec.insert( shapevec.end(), shapelist.begin(), shapelist.end() );
    
      shapevec.erase(remove_if( shapevec.begin(), shapelist.end(), CloseTo( Vertex(6, 7)) ), shapevec.end() );
      ostream_iterator<const shapePtr>sahpeout(cout, "\n");
      cout << sahpevec.size() << endl;
      cerr << shapePtr::numshapes << endl;
      copy( shapevec.begin(), shapevec.end(), shapecout );
    
      cout << endl;
      return 0;
    }
    I attached the completed code.
    Thank You for hint and help.

    Adalte.
    Last edited by Adalte; 07-10-2009 at 06:51 PM.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    vector<type> variable_name (number_of_elements);
    ShapePtr is not a type, it is a variable
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Furthermore, main must return int. In your code, it returns nothing. This is invalid in C++.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Thank you for hints.

    I changed code (just in main, all other is the same):
    The problem now is how to put shapes (Polygons, Rectangles, etc) in the vector shapevec. I get error:

    "NewShapes.cpp:11: error: no matching function for call to `ShapePtr::ShapePtr(Polygon*)'"

    Main:
    Code:
    #include <vector>
    #include <fstream>
    #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)) );
      ...
    }

    The class ShapePtr.h (I changed name Shape (h and cpp) to ShapePtr):

    Code:
    #ifndef SHAPEPTR_H
    #define SHAPEPTR_H
    #include <iostream>
    #include <string>
    #include "Vertex.h"
    using namespace std;
    
    class ShapePtr {
      public:
        ShapePtr() {}
        ~ShapePtr() {}
    };
    #endif

    ShapePtr.cpp:

    Code:
    #include <iostream>
    #include <cmath>
    #include "Vertex.h"
    #include "ShapePtr.h"
    using namespace std;
    
    class Polygon : public ShapePtr {
      private:
        Vertex* Pol;
        int storlek, pX, pY;
      public:
        Polygon::Polygon(int x = 0, int y = 0, Vertex* varr = 0, int num = 0) : pX(x), pY(y), storlek(num) {
          if (storlek > 0) {
             Pol = new Vertex[storlek];
             for (int ix=0; ix<storlek; ix++)
                 Pol[ix] = varr[ix];
           } else Pol = 0;
        }
        virtual Polygon::~Polygon() { delete[] Pol; }
    };
    I understand that I need a constructor in ShapePtr that get a pointer of shape (Polygon, etc): how is code like, I wander.
    Please help me.

    Adalte.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First and foremost, main must return int! I told you before, but I see no change.
    Second, never ever include .cpp files. They are compiled separately.
    And thirdly, about your approach, do you intend to store different shapes (ie rectangles, polygons, etc) into a single vector and use them all from there?
    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.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Yes Elysia You are right, I intend to store different shapes (polygons, rectangles, etc) into a single vector (if it's possible, it's not a must) and then use them all from there. How I can do that? Do You can help me with this, please?

    Now are .cpp files not includes.

    Thank You in advance.

    Adalte.
    Last edited by Adalte; 07-12-2009 at 11:12 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can do that with some polymorphism. Do you know how that works?
    You must store pointers in your vector. Then you can create objects of any type that is derived from the type you're storing in the vector and store them there. Remember that you won't actually know the type of the objects stored, so you would have to use virtual functions.

    In the current code, you get a compile error because ShapePtr does not have a constructor that takes a Polygon* argument. But you won't need to anyway.
    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.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well, I see, OP, you're using both a vector and a list. In this case you could use one or the other, because both vectors and lists can be looped over and displayed on screen, if that's all you intend to do.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't name the ShapePtr class with Ptr on the end unless it is some kind of smart pointer, which it is not.
    Vectors cannot be used polymorphically. You would need to declare the vector to store actual pointers to objects instead.

    Making the Polygon destructor virtual wont help unless you make the ShapePtr destructor virtual.

    The Polygon class needs to follow the rule of three. Either write a copy-constructor and asignment operator or declare them private and leave them unimplemented.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Hi !

    Thank all You!
    I solve the question but I'm in the beginning of this task: I still need You help. I started a new thread: "I need help, please".

    Thank You in advanced.

    Adalte.

    Pd: You was right iMalc !

Popular pages Recent additions subscribe to a feed