473,383 Members | 1,846 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,383 software developers and data experts.

getting syntax error !!

hey, i have been writing this code (C++) in the last week,
i have many classes in the .h file, but one problem with one class, which is:
__________________________________________________ _________

35 template <int size=10>
36 class Picture
37 {
38 vector<Shape*> vec;
39
40 public:
41 void add(Shape* s)
42 {
43 if (s != NULL) vec.push_back(s);
44 return;
45 }
46 Picture();
47 ~Picture();
48 void print_all();
49 void add(unsigned int i, Shape* s);
50 void remove(unsigned int i);
51 void print(unsigned int i);
52 void extend_all(int i);
53 void extend(unsigned int i, int x);
54 void move_all(int x, int y);
55 void move(unsigned int i, int x, int y);
56 };
__________________________________________________ __________

when trying to build i'm getting this errors:
1>c:\users\ahmad\desktop\shapes\shapes\shapes.h(38 ): error C2143: syntax error : missing ';' before '<'
1> c:\users\ahmad\desktop\shapes\shapes\shapes.h(56) : see reference to class template instantiation 'Picture<size>' being compiled
1>c:\users\ahmad\desktop\shapes\shapes\shapes.h(38 ): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ahmad\desktop\shapes\shapes\shapes.h(38 ): error C2238: unexpected token(s) preceding ';'
May 25 '12 #1
6 2091
Banfa
9,065 Expert Mod 8TB
It's hard to say without the rest of the file but here are some clues
  • Where is Shape defined, it needs to be earlier in the file than this class?
  • Have you included vector into this file?
  • Do you have a using namespace std; in the file? You shouldn't it is not good practice but if you haven't you need to declare your vector std::vector.
May 25 '12 #2
here's how the code starts:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. using namespace std;
  3.  
  4. template <class T> 
  5. class Point
  6. {
  7. public:
  8.     T x;
  9.     T y;
  10.     Point(T _x=0, T _y=0): x(_x), y(_y){}
  11.     void move(T _X, T _y)
  12.     {
  13.         x += _X;
  14.         y += _y;
  15.     }
  16. };
  17. /******************************************************/
  18. class Shape
  19. {
  20. public:
  21.     virtual void print()=0;
  22.     virtual Shape* clone()=0;
  23.     virtual void extend(int i)=0;
  24.     virtual void move(int x,int y)=0;
  25. };
  26. /******************************************************/
  27. template <int size=10>
  28. class Picture
  29. {
  30.     vector<Shape*> vec;
  31.  
  32. public:
  33.     void add(Shape* s)
  34.     { 
  35.         if (s != NULL) vec.push_back(s);
  36.         return;
  37.     }
  38.     Picture();
  39.     ~Picture();
  40.     void print_all();
  41.     void add(unsigned int i, Shape* s);
  42.     void remove(unsigned int i);
  43.     void print(unsigned int i);
  44.     void extend_all(int i);
  45.     void extend(unsigned int i, int x);
  46.     void move_all(int x, int y);
  47.     void move(unsigned int i, int x, int y);
  48. };
  49. /******************************************************/
  50. template <class X>
  51. class Circle: public Shape 
  52. {
  53. protected:
  54.     X radius;
  55.     Point<X> center;
  56.     static const X step; 
  57.  
  58. public:
  59.     Circle();
  60.     Circle(X, X, X); 
  61.     Circle(Point<X>, X);
  62.     virtual void print();
  63.     virtual Circle<X>* clone();
  64.     virtual void extend(int i);
  65.     virtual void move(int x,int y);
  66. };
  67. /******************************************************/
  68. template <class X>
  69. class Square: public Shape 
  70. {
  71. protected:
  72.     Point<X> point;
  73.     X limp;
  74.     static const X step;
  75.  
  76. public:
  77.     Square();
  78.     Square(X, X, X);
  79.     Square(Point<X>, X);
  80.     virtual void print();
  81.     virtual Square<X>* clone();
  82.     virtual void extend(int i);
  83.     virtual void move(int x, int y);
  84. };
  85. /******************************************************/
  86. template <class X>
  87. class Triangle: public Shape 
  88. {
  89. private:
  90.     Point<X> head_1, head_2, head_3;
  91.     static const X step;
  92.  
  93. public:
  94.     Triangle();
  95.     Triangle(X, X, X, X, X, X);
  96.     Triangle(Point<X>, Point<X>, Point<X>);
  97.     void print();
  98.     void extend(int i);
  99.     Triangle<X>* clone();
  100.     void move(int x, int y);
  101. };
  102. /******************************************************/
  103. template <class X>
  104. class Ellipse: public Circle<X>
  105. {
  106. protected:
  107.     X radius_2;
  108.  
  109. public:
  110.     Ellipse();
  111.     Ellipse(X, X, X, X);
  112.     Ellipse(Point<X>, X, X);
  113.     void print();
  114.     Ellipse<X>* clone();
  115.     void extend(int i);
  116.     void move(int x, int y);
  117. };
  118. /******************************************************/
  119. template <class X>
  120. class Rectangle: public Square<X>
  121. {
  122. private:
  123.     X vertical;
  124.  
  125. public:
  126.     Rectangle();
  127.     Rectangle(X, X, X, X);
  128.     Rectangle(Point<X>, X, X);
  129.     void print();
  130.     Rectangle<X>* clone();
  131.     void extend(int i);
  132.     void move(int x, int y);
  133. };
  134. /******************************************************/
  135.  
i didn't use #include<vector> and using namespace std;, then the compiler gave the errors in first post.
now, after adding these (#include<..> and using namespace..) it gives linker errors !

here are the errors:
1>shapes.obj : error LNK2001: unresolved external symbol "private: static char const Triangle<char>::step" (?step@?$Triangle@D@@0DB)
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Picture<6>::~Picture<6>(void)" (??1?$Picture@$05@@QAE@XZ) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::add(unsigned int,class Shape *)" (?add@?$Picture@$05@@QAEXIPAVShape@@@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Triangle<int>::Triangle<int>(int,int,int,int,int,i nt)" (??0?$Triangle@H@@QAE@HHHHHH@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::extend_all(int)" (?extend_all@?$Picture@$05@@QAEXH@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::remove(unsigned int)" (?remove@?$Picture@$05@@QAEXI@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::move(unsigned int,int,int)" (?move@?$Picture@$05@@QAEXIHH@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::move_all(int,int)" (?move_all@?$Picture@$05@@QAEXHH@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::print(unsigned int)" (?print@?$Picture@$05@@QAEXI@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Picture<6>::print_all(void)" (?print_all@?$Picture@$05@@QAEXXZ) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Triangle<char>::Triangle<char>(void)" (??0?$Triangle@D@@QAE@XZ) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Picture<6>::Picture<6>(void)" (??0?$Picture@$05@@QAE@XZ) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle<long>::Rectangle<long>(class Point<long>,long,long)" (??0?$Rectangle@J@@QAE@V?$Point@J@@JJ@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Square<unsigned int>::Square<unsigned int>(unsigned int,unsigned int,unsigned int)" (??0?$Square@I@@QAE@III@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Ellipse<double>::Ellipse<double>(class Point<double>,double,double)" (??0?$Ellipse@N@@QAE@V?$Point@N@@NN@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle<char>::Circle<char>(char,char,char)" (??0?$Circle@D@@QAE@DDD@Z) referenced in function _main
1>shapesMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle<int>::Circle<int>(void)" (??0?$Circle@H@@QAE@XZ) referenced in function _main

and here's my main:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "shapes.h"
  3.  
  4.  
  5. template<> const int Circle<int>::step = 1;
  6. template<> const double Circle<double>::step = 0.1;
  7.  
  8. template<> const int Square<int>::step = 1;
  9. template<> const double Square<double>::step = 0.1;
  10.  
  11. template<> const int Triangle<int>::step = 1; 
  12. template<> const double Triangle<double>::step = 0.1;
  13.  
  14.  
  15. int main()
  16. {
  17.     Circle<int> *c = new Circle<int>();
  18.     c->print();
  19.     Circle<char> *ch = new Circle<char>('2','3','3');
  20.     ch->print();
  21.     Ellipse<double> *e = new Ellipse<double>(Point<double>(1,2),2.2,5.6);
  22.     e->print();
  23.     Square<unsigned int> *s = new Square<unsigned int>(3,4,5);
  24.     s->print();
  25.     Rectangle<long> *r = new Rectangle<long>(Point<long>(1,3),2,3);
  26.     r->print();
  27.     cin.get();
  28.      Picture<6> pic;
  29.  
  30.     pic.add(c);
  31.     pic.add(e);
  32.     pic.add(ch);
  33.     pic.add(s);
  34.     pic.add(r);
  35.     delete c;
  36.     delete ch;
  37.     delete e;
  38.     delete s;
  39.     delete r;
  40.     pic.add(new Triangle<char>());
  41.     pic.print_all();
  42.     pic.print(2);
  43.     pic.move_all(20,3);
  44.     pic.move(2,5,6);
  45.     pic.remove(3);
  46.     pic.print_all();
  47.     pic.extend_all(-2);
  48.     pic.add(4,new Triangle<int>(2,3,4,4,5,5));
  49.  
  50.     return 0;
  51. }
  52.  
May 25 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
Declaring a static variable inside a class does not create the variable.

Remember that a static variable is not a member variable. it has no this pointer. That's so that you don't create another static variable each time you create a class object.

You will need to create your static variable outside the class template.
May 25 '12 #4
thank your for your response,
but you mean by "outside the class template" ??, each class has its own "step", e.g Circle<int> has its step which is 1, and Circle<double> has another one, which is 0.1
sorry but i'm new for C++, so can you please explain more :)
May 25 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
There can be only one static variable named step for all of your objects. Every object you create will use the same static variable. This appears to not meet your needs.

Therefore, don't use a static variable.

Since each class has its own step, then just make the step a regular member variable. Use a constructor to inialize the variable.

Since your variable is const, then you need to initialize it by using the initialization list of your constructors.

You could pass the step value to the constructor when you create the object:

Expand|Select|Wrap|Line Numbers
  1. Circle<double> cir(0.1);
  2. Circle<int> anothercir(1);
You might consider using an enum for the step rather than a template paramter type.
May 25 '12 #6
ok thank you very, :)
i have fixed it :)
May 26 '12 #7

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

Similar topics

3
by: kufre | last post by:
I need some fresh eyes to take a look at this code for me and let me know what it is I'm doing wrong because I keep getting all kinds of error message with this code. I've had several people help...
2
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable...
14
by: group8perl | last post by:
am getting syntax error, can someone please explain how to insert a hyperlink into a table.
18
by: rahuls | last post by:
Hi everyone, I have a couple of questions? 1.this is part of my main function ////////////////////////////// int main() { FILE *f1; int i,j; j=100000000; int intData;
1
by: ruvi | last post by:
Hi, I am using Vb 6 and Access. I am getting syntax error in the following sql query. strSql = "SELECT ...
3
by: silambu | last post by:
hi,can anybody tell reason for getting syntax error near , while either updating or inserting records in table
3
paulrajj
by: paulrajj | last post by:
hi to all, i am getting syntax error on my code.. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in D:\xampp\htdocs\Dummy\paulraj\matrim\exam.php on line 62 ...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
1
by: inbarik | last post by:
Hi, i'm getting syntax error message for this button that tries to get name (of building) with apostrophe. Any idea how to solve this? Note that i must use name with apostrophe e.g. O'neal: ...
0
by: dougancil | last post by:
I have the following code: Imports System.Data.SqlClient Public Class Main Protected WithEvents DataGridView1 As DataGridView Dim instForm2 As New Exceptions Private Sub...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.