String declaration in class definition

Reply

Join Date: Dec 2004
Posts: 4,177
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 479
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

String declaration in class definition

 
0
  #1
May 6th, 2005
I'm trying to create class/header for my program which should calculate distanaces between cities declared by array "use" plus arrays of there coordinates as you see in code below
  1. // file line.h
  2.  
  3. #ifndef LINE_H
  4. #define LINE_H
  5. const int cities = 6;
  6.  
  7. class Line
  8. {
  9. private:
  10. string use[] = {"Phoenix", "Baton Rouge", "Bismark", "Columbia", "Olympia", "Springfield"}; //predefined parameters name of cities
  11. int x[] = {380, 890, 650, 1140, 70, 920}; // x coordinate of each city
  12. int y[] = {280, 180, 690, 300, 700, 470}; // y coordinate of each city
  13.  
  14. string dest[] = {"Phoenix"}; //cities which been used in calculation, as a target city and starting city
  15. int dist[]; //calculated distanced between cities
  16.  
  17. int possition[] = {0, 1, 2, 3, 4, 5}; //to be use for swap function, after swaping to find city with shortest distatce from start city
  18. int calc[]; //array to hold temporary distances
  19. int x1;
  20. int x2;
  21. int y1;
  22. int y2;
  23.  
  24. public:
  25. Line();
  26. void calculate();
  27. void find();
  28. bool notInDest();
  29. void sort();
  30. void swap();
  31. void smallest();
  32. void display();
  33. };
  34.  
  35. #endif;
I created prototypes of functions in cc program and tryed to compile, but got this error messages
  1. line.h:10: syntax error before `['
  2. line.h:10: missing ';' before right brace
  3. line.h:14: syntax error before `['
  4. line.h:24: parse error before `public'
  5. line.h:33: parse error before `}'
  6. line.cc:6: definition of implicitly-declared `Line::Line()'
  7. line.cc:6: declaration of `Line::Line()' outside of class is not definition
  8. line.cc:9: no `void Line::find()' member function declared in class `Line'
  9. line.cc:12: parse error before `bool'
  10. line.cc:16: parse error before `void'
  11. line.cc:20: parse error before `void'
  12. line.cc:24: parse error before `void'
  13. line.h:15: storage size of `dist' isn't known
  14. line.h:18: storage size of `calc' isn't known
My main concern is the declaration of string use and dest. Can you please explain me what I did wrong?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: String declaration in class definition

 
0
  #2
May 6th, 2005
1) You need to include <string> because you try to use the string class.
2) You need to qualify each use of string with std::.
3) You can't initialize arrays in a class declaration.
4) Because of 3, you must supply a size for the arrays.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC