944,048 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19973
  • C RSS
May 6th, 2005
0

String declaration in class definition

Expand Post »
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?
Similar Threads
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
May 6th, 2005
0

Re: String declaration in class definition

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: [basics] LZ(W) compression explanation.
Next Thread in C Forum Timeline: Analyzing the Round Robin Scheduling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC