C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 06:45 PM   #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.
Attached Files
File Type: cpp NewShapes.cpp (1.2 KB, 10 views)
File Type: h Vertex.h (284 Bytes, 10 views)
File Type: h Shape.h (168 Bytes, 11 views)
File Type: cpp Shape.cpp (512 Bytes, 10 views)
File Type: h Listan.h (201 Bytes, 11 views)

Last edited by Adalte; 07-10-2009 at 06:51 PM.
Adalte is offline   Reply With Quote
Old 07-10-2009, 07:02 PM   #2
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Code:
vector<type> variable_name (number_of_elements);
ShapePtr is not a type, it is a variable
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-11-2009, 08:09 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2009, 03:50 PM   #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.
Adalte is offline   Reply With Quote
Old 07-12-2009, 02:54 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-12-2009, 11:08 AM   #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   Reply With Quote
Old 07-12-2009, 11:18 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-12-2009, 11:23 AM   #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   Reply With Quote
Old 07-12-2009, 01:32 PM   #9
Algorithm Dissector
 
iMalc's Avatar
 
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   Reply With Quote
Old 07-12-2009, 03:28 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 07:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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