473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with standard in copy/ostream_iterator

9
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"

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

NewShapes.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <fstream>
  3. #include <string>
  4. #include "Vertex.h"
  5. #include "Shape.cpp"
  6. #include "Listan.h"
  7. using namespace std;
  8.  
  9. main() {
  10.   cout << endl;
  11.   Listan ShapePtr;
  12.   vector<ShapePtr> shapevec;
  13.   Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
  14.   shapevec.push_back( shapePtr(new Polygon(1, 4, varr, 4)) );
  15.   shapevec.push_back( shapePtr(new Circle(5, 5, 4)) );
  16.   shapevec.push_back( shapePtr(new Rectangle(4, 10, 2, 4)) );
  17.   shapevec.push_back( shapePtr(new Point(6, 7, 1)) );
  18.  
  19.   ofstream is("fil.dat");
  20.   ostream_iterator<const shapePtr> shapeout(os, "\n");
  21.   copy( shapevec.begin(), sahpevec.end(), shapeout);
  22.   os.close();
  23.  
  24.   ifstream is("fil.dat");
  25.   for (list<shapePtr>::iterator it = shapelist.begin(); it != shapelist.end(); it++)
  26.       cout << *it << endl;
  27.   shapevec.insert( shapevec.end(), shapelist.begin(), shapelist.end() );
  28.  
  29.   shapevec.erase(remove_if( shapevec.begin(), shapelist.end(), CloseTo( Vertex(6, 7)) ), shapevec.end() );
  30.   ostream_iterator<const shapePtr>sahpeout(cout, "\n");
  31.   cout << sahpevec.size() << endl;
  32.   cerr << shapePtr::numshapes << endl;
  33.   copy( shapevec.begin(), shapevec.end(), shapecout );
  34.  
  35.   cout << endl;
  36.   return 0;
  37. }
  38.  
I attached the complete code.
Thank You for hints and help.

Adalte.
Attached Files
File Type: txt NewShapes_cpp.txt (1.2 KB, 463 views)
File Type: txt Vertex_h.txt (284 Bytes, 340 views)
File Type: txt Shape_h.txt (168 Bytes, 361 views)
File Type: txt Shape_cpp.txt (512 Bytes, 357 views)
File Type: txt Listan_h.txt (201 Bytes, 362 views)
Jul 11 '09 #1
20 3225
Banfa
9,065 Expert Mod 8TB
The error messages highlight the problem
Expand|Select|Wrap|Line Numbers
  1.    Listan ShapePtr;
  2.    vector<ShapePtr> shapevec;
You have passed ShapePtr as a template parameter but template parameters need to be types on the whole or occasionally constant values (const int, const float etc).

The error messages say all this. It is worth reading about and understanding the diagnostic message produced by your compiler.
Jul 11 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
Also, you do not #include .cpp files.

What you do is ADD .cpp files to your project. Each .cpp file must be compiled separately. The makefile takes care of compiling each .cpp file.

By #including .cpp files, you duplicate the code each time you compile. AT some point youo will start gettingr redefinition errors and your builds will stop working.
Jul 11 '09 #3
Adalte
9
Thank you for hints.
Manfa, you mean: "vector<const ShapePtr> shapevec"? Thats not work ...
weaknessforcats, all CPP compiles: there are just two, the others headers has inline code.

The problem still there ... Plaese help ...

Adalte.
Jul 11 '09 #4
Adalte
9
Hi !

I change the code. Now I get the following error:
"NewShapes.cpp:11: error: no matching function for call to `ShapePtr::ShapePtr(Polygon*)'"

How I can to put shapes (Polygons, Rectangles, etc) in the vector shapevec, how the code like, I wander.

Main:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <fstream>
  3. #include <string>
  4. #include "Vertex.h"
  5. #include "ShapePtr.cpp"
  6. using namespace std;
  7.  
  8. main() {
  9.   vector<ShapePtr> shapevec;
  10.   Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
  11.   shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
  12.   ...
  13.  
The class ShapePtr.h (I change name Shape (h and cpp) to ShapePtr):
Expand|Select|Wrap|Line Numbers
  1. #ifndef SHAPEPTR_H
  2. #define SHAPEPTR_H
  3. #include <iostream>
  4. #include <string>
  5. #include "Vertex.h"
  6. using namespace std;
  7.  
  8. class ShapePtr {
  9.   public:
  10.     ShapePtr() {}
  11.     ~ShapePtr() {}
  12. };
  13. #endif
  14.  
ShapePtr.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. #include "Vertex.h"
  4. #include "ShapePtr.h"
  5. using namespace std;
  6.  
  7. class Polygon : public ShapePtr {
  8.   private:
  9.     Vertex* Pol;
  10.     int storlek, pX, pY;
  11.   public:
  12.     Polygon::Polygon(int x = 0, int y = 0, Vertex* varr = 0, int num = 0) : pX(x), pY(y), storlek(num) {
  13. << pY << endl;
  14.       if (storlek > 0) {
  15.          Pol = new Vertex[storlek];
  16.          for (int ix=0; ix<storlek; ix++)
  17.              Pol[ix] = varr[ix];
  18.        } else Pol = 0;
  19.     }
  20.     virtual Polygon::~Polygon() { delete[] Pol; }
  21. };
  22.  
Help me, please.

Adalte.
Jul 11 '09 #5
newb16
687 512MB
Why do you inherit Polygon from ShapePtr? As I understand, you need to implement ( or use the already implemented ) shared pointer class, create a vector<shared_ptr<Shape> >;
and inherit polygon and other shapes from basic class Shape, so that the vector contains elements of the same type.
Jul 12 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
Thats not work ...
weaknessforcats, all CPP compiles: there are just two, the others headers has inline code.
Again, do not #include .cpp files.

It doesn't work.

Example:

Expand|Select|Wrap|Line Numbers
  1. X. cpp
  2.  
  3. int data = 10;
  4.  
  5. Y.cpp
  6. #include "X.cpp"
  7.  
  8. Z.cpp
  9. #include "Y.cpp"
  10. int main()
  11. {
  12.  
  13. }
This dies in the link with a redefinition of int data.

All files that you #include cannot: a) allocate memory or b) contain any code that causes the compiler to generate instructions. You #include files that cotain only declarations rather than definitions.
Jul 12 '09 #7
Banfa
9,065 Expert Mod 8TB
I think in this case the mistake is not in including a cpp file (which is a mistake and should not be done) but in the file name itself.

Examining the code in the cpp file being included it is apparent that it contains a class declaration, that is statements you would normally expect to find in a header file. The mistake is in giving what is basically a header file the .cpp extension.

If you want to have a vector of polymorphic types, that is a vector of objects that exhibit the same interface but differences in how each concrete object implements the functionality you need to have a vector of pointers to some base class that all your polymorphic objects inherit from.

ShapePtr could be your base class and Polygon does inherit from it, there are some problems with this, shape pointer does not define an object interface (there are no virtual functions) and if you are intending that a class is to be used as the base of an inheritance tree of polymorphic types that will be accessed through a base class pointer then the destructor MUST be declared virtual.

Also instead of creating a vector of base class pointers you declare a vector of base classes. None of the objects in you vector will/can have the functionality of any of the derived classes (Polygon) because the base, ShapePtr, does not implement that functionality and you are storing the base class not a base class pointer to a derived class.

This code construct

ShapePtr(new Polygon(1, 4, varr, 4))

should be a give away that something is wrong. Polygon is derived from ShapePtr so I could do this legally

Expand|Select|Wrap|Line Numbers
  1. Polygon *pg = new Polygon(1, 4, varr, 4);
  2. ShapePtr *ptr = pg;
  3.  
  4. // or indeed this
  5.  
  6. ShapePtr obj(static_cast<ShapePtr>(pg));
  7.  
The last of those is fairly pointless in this case because ShapePtr has no data members to copy so it is actually the equivalent of

ShapePtr obj;

in this specific case.

I suspect you need to do the following:

1. Declare shapevec as

vector<ShapePtr *> shapevec;

2. Alter the declaration of ShapePtr to actually declare your polymorphic interface in it.
Jul 12 '09 #8
Adalte
9
Hi !

Thank You for hints.
The .cpp files are now not included.

Newb16, I'm not sure I understand You but I think it's my fault: I changed the classes name from Shape to ShapePtr, I'm sorry that I confuse You. I try to create a vector (named shapevec) and put there some shapes (polygons, rectangles, etc). Then I'll be sort them, remove some (according to size or position), save them on a file and read them from file.

I'm just in the beginning of this task, please help me.
Thank You in advance.

Adalte.
Jul 12 '09 #9
Adalte
9
Hi !

Banfa You are right. I implement a base class, ShapePtr.h, from other project and shapes (polygons, rectangles, etc) inherits (in ShapePtr.cpp) from it.

I solvet the problem. The constructor I need was:
ShapePtr(ShapePtr* ptr)
... its look like to work.

I'm in the beginning of this task, please help me. I started an other thread to continue there: "Help, please".

Thank You in advance.

Adalte.
Jul 12 '09 #10
Adalte
9
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:
Expand|Select|Wrap|Line Numbers
  1. "C:/Qt2009_02/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stream_iterator.h: In member fu
  2. nction `std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [wi
  3. th _Tp = const ShapePtr, _CharT = char, _Traits = std::char_traits<char>]':" ...
Here is Main (NewShapes.cpp):
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <string>
  5. #include "Vertex.h"
  6. #include "ShapePtr.cpp"
  7. using namespace std;
  8.  
  9. main() {
  10.   vector<ShapePtr> shapevec;
  11. cout << "  Main_BeforeVertices" << endl;
  12.   Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
  13.   shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
  14.   shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );
  15.   shapevec.push_back( ShapePtr(new Rectangle(4, 10, 2, 4)) );
  16.   shapevec.push_back( ShapePtr(new Point(6, 7, 1)) );
  17.  
  18.   ofstream os("fil.dat");
  19.   ostream_iterator<const ShapePtr> shapeout(os, "\n");
  20.   copy(shapevec.begin(), shapevec.end(), shapeout);
  21.   os.close();
  22. }
  23.  
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.
Attached Files
File Type: txt NewShapes_cpp.txt (658 Bytes, 363 views)
File Type: txt ShapePtr_h.txt (245 Bytes, 360 views)
File Type: txt ShapePtr_cpp.txt (1.1 KB, 392 views)
File Type: txt Vertex_h.txt (305 Bytes, 332 views)
File Type: txt error.txt (9.3 KB, 404 views)
Jul 12 '09 #11
Banfa
9,065 Expert Mod 8TB
Adalte,

I have renamed this thread, we prefer our threads to have meaningful names and "I need help" has no meaning in relation to the problem. You should probably take a little time to read our posting guidelines.

I have merged your other thread into this one since it is not really a separate problem.

You have not taken on board what has been said about icluding files. If the file is written to be included into other files then it should not have a .cpp extension but a .h extension.

We can not help you with an error message if you only post half of it.

Even if your code compiled I would be surprised if you ended up with any data in your output file because the class ShapePtr has no data in it.

You have not taken on board what I said about vectors and the type you are storing in the vector in your code.
Jul 13 '09 #12
donbock
2,426 Expert 2GB
@Banfa
This file naming convention is not required by the language standard but there is near universal compliance within the user community. Violating this convention assures maximum confusion when others look at your code.
Jul 13 '09 #13
Adalte
9
@Banfa
Right. Do You can rename it to something like "Problem with standard in copy/ostream_iterator", please?

@Banfa
I agreed with donbock. I didn't think about it but about the self problem. The extension name is correct now.

@Banfa
I don't understand it. In my last post, I write that I solve the first problem, I write the code (solution) and I attach a file with all error message. You can read that post.

@Banfa
If You are right, may be it can help me. May be You can explain it for me. I check that the ShapePtr's constructor is called after I put each shape (4 shapes, 4 calls) and ShapePtr's destructor is called (4 times) before the program is terminated.

@Banfa
May be You forgot what I wrote in my first post: I can not change the code in main but I may add a new code to the main. I may change, delete and add code to the others files.

My problem now is copy-command. If I remark it, the code compiles without errors else with very large error.

I attached the files.

It's the main (NewShapes.cpp) now:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <string>
  6. #include "Vertex.h"
  7. #include "Shapes.h"
  8. using namespace std;
  9.  
  10. int main() {
  11.   vector<ShapePtr> shapevec;
  12.   Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
  13.   shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
  14.   shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );
  15.   shapevec.push_back( ShapePtr(new Rectangle(4, 10, 2, 4)) );
  16.   shapevec.push_back( ShapePtr(new Point(6, 7, 1)) );
  17.   ofstream os("fildat.txt");
  18.   ostream_iterator<const ShapePtr> shapeout(os, "\n");
  19.  
  20.   copy(shapevec.begin(), shapevec.end(), shapeout); //  <-- !!!
  21.  
  22.   os.close();
  23. }
  24.  
Adalte.
Attached Files
File Type: txt error.txt (9.3 KB, 359 views)
File Type: txt ShapePtr-h.txt (247 Bytes, 358 views)
File Type: txt Shapes-h.txt (1.1 KB, 352 views)
File Type: txt Vertex-h.txt (299 Bytes, 320 views)
File Type: txt NewShapes-cpp.txt (710 Bytes, 357 views)
Jul 14 '09 #14
donbock
2,426 Expert 2GB
@Adalte
It is not uncommon to have the constraint that a certain preexisting file can't be changed; but neither of the more common reasons for that constraint seem to apply in your case.
  1. The most common reason is that the source file has been lost. That obviously isn't the case.
  2. Sometimes a source file belongs to somebody else and there are legal reasons why you can't alter it. However, you're willing to "add new code".
How do you perceive the distinction between changing the code and adding new code? Are you saying that you are allowed to intersperse new lines among the original lines, but that you could reconstitute the original file merely be deleting the new lines? Are you allowed to effectively delete original lines by bracketing them with "#if 0" and "#endif"? What is the underlying reason for this constraint?
Jul 14 '09 #15
newb16
687 512MB
@donbock
The reason is to force students to use specific constructions, i.e. ostream_iterator, and implement operator << for their specific class to make ostream_iterator compile. Without this restriction, they will just loop through the container and invoke some TheirClass::output() method for each element.
Jul 14 '09 #16
Banfa
9,065 Expert Mod 8TB
@Adalte
Are you saying you get errors? If so post them in full.

Sorry forgot about not being able to change main but given that you have a vector of ShapePtr objects so when you do this

shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );

what do you think happens, explain it in English (or, for your own purposes only, in your first language if that is not English)? What happens in the ShapePtr object created when it is initialised from the Polygon? What do you think gets put into the vector?

Read the documentation for ostream_iterator, if you want to be able to do this

[/code]
ostream_iterator<const ShapePtr> shapeout(os, "\n");

copy(shapevec.begin(), shapevec.end(), shapeout); // <-- !!!
[/code]

the this

cout << ShapePtr;

has to be valid. Is that valid for your code? If it was valid what data would be being output for ShapePtr?


And a final point, although this is not preventing the code from working and it is in main so not code written by yourself but if you new something then at some point you should delete it before the program exits.
Jul 14 '09 #17
Adalte
9
Sorry but my english is not so god.

@Banfa
I did. You can see it in my attached files: error.txt

@Banfa
I see what You mean: ShapePtr's defaultconstructor is called, then Polygon's constructor, then Vertex's constructor, then ShapePtr's constructor and last ShapePtr's destructor: Here is the problem. Why ShapePtr's destructor is called here I don't understand. How I can avoid it?

@Banfa
I have read it and read the examples: The syntax is the same but with an other variables names. Or ...?

What do You think I have to do to keep the objects in the vector, so that I can save them in a file?

Adalte.
Jul 15 '09 #18
weaknessforcats
9,208 Expert Mod 8TB
I see what You mean: ShapePtr's defaultconstructor is called, then Polygon's constructor, then Vertex's constructor, then ShapePtr's constructor and last ShapePtr's destructor: Here is the problem. Why ShapePtr's destructor is called here I don't understand. How I can avoid it?
The destructor of ShapePtr is called because you have a pointer to ShapePtr.

I realize that if you really have the address of a Polygon in your ShapePtr that you should be calling the Polygon destructor instead.

You make this happen by using a virtual destructor in the ShapePtr class.

Actually, you should have a Shape class:

Expand|Select|Wrap|Line Numbers
  1. class Shape
  2. {
  3.  
  4.     public:
  5.        virtual ~Shape();                //virtual destructor
  6. };
  7. class Polygon: public Shape
  8. {
  9.  
  10. };
rather than ShapePtr. Your polygon is:

Expand|Select|Wrap|Line Numbers
  1. Shape* myShape = new Polygon();
and your vector of shapes is a vector of Shape pointers:

Expand|Select|Wrap|Line Numbers
  1. vector<Shape*> myShapes;
  2. myShapes.push_back( new Polygon());
Jul 15 '09 #19
Banfa
9,065 Expert Mod 8TB
@weaknessforcats
If you read the thread (and it is long and converluted so I undersand why you didn't) you would see we have already had this argument, however he can not change the main.cpp where the vector is defined as

vector<ShapePtr> myShapes;

Personally I think it is a mistake to subclass from ShapePtr, I think there should be a different base class and that ShapePtr should be written in the form of the Handle Pattern to hold that other base class pointer.

ShapePtr would then also need the methods required to let the ostream_iterator to work.
Jul 15 '09 #20
Adalte
9
weaknessforcats, banfa let's go: we try weaknessforcats idea. But weaknessforcats may should read my last uploaded files: there is not Shape class, may be You mean ShapePtr?
Then, You write:
Expand|Select|Wrap|Line Numbers
  1. Shape* myShape = new Polygon();
  2. vector<Shape*> myShapes;
  3. myShapes.push_back( new Polygon());
  4.  
1. Mean You an empty Polygon() ?
2. What does Your pointer "myShape" ? (Sorry, I don't get it)
3. If we will put pointers of shapes (Polygon, Rectangle, etc) into the vector, why we need pointers to that shapes and do nothing with them? Or You mean
4. If we will have "vector of shapes is a vector of Shape pointers", isn't the code as follow?:
Expand|Select|Wrap|Line Numbers
  1. ShapePtr* myShape = new Polygon();
  2. vector<ShapePtr*> shapesvec;
  3. shapevec.push_back(myShape);
  4.  
Please, understand me right: I try to understand what You two mean, which I appreciate it and thank very much.

Adalte.
Jul 15 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris Mantoulidis | last post by:
Hello all... I get an error message for the following program by the compiler: #include <iostream> #include <string> using namespace std; ostream_iterator<string> oo(cout);
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
5
by: plmanikandan | last post by:
Hi, I need to split the value stored in a string and store them to another charrecter array.I am using strtok function.But i am getting invalid output when there is no value between delimiter ...
4
by: fightwater | last post by:
i wrote a program which uses the stl compose1 functor but the compiler seems cannot find it here is the code ////////////////////////////////////////// #include <vector> #include <numeric>...
2
by: alzforu | last post by:
The code in which i m facing the problem is: //file stream.cc #include <vector> #include <algorithm> #include <iostream> using namespace std;
2
by: toton | last post by:
Hi, I am trying to use boost::range with one of my own container class, and having some problem. I am missing some usage of range. Can anyone suggest a proper way for it ? To show the problem...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
5
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
2
by: 032386 | last post by:
#include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <functional> using namespace std; void reverse(string& s)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.