![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 6
| Help, please 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;
}
Thank You for hint and help. Adalte. Last edited by Adalte; 07-10-2009 at 06:51 PM. |
| Adalte is offline | |
| | #2 |
| Banned Join Date: Mar 2009
Posts: 533
| Code: vector<type> variable_name (number_of_elements);
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Furthermore, main must return int. In your code, it returns nothing. This is invalid in C++.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #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; }
};
Please help me. Adalte. |
| Adalte is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #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. |
| Adalte is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #8 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| 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.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #9 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| 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. |
| iMalc is offline | |
| | #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 ! |
| Adalte is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|