DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   String declaration in class definition (http://www.daniweb.com/forums/thread23157.html)

peter_budo May 6th, 2005 7:22 pm
String declaration in class definition
 
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
// file line.h

#ifndef LINE_H
#define LINE_H
const int cities = 6;

class Line
{
 private:
  string use[] = {"Phoenix", "Baton Rouge", "Bismark", "Columbia", "Olympia", "Springfield"};    //predefined parameters name of cities
  int x[] = {380, 890, 650, 1140, 70, 920};                                                        // x coordinate of each city
  int y[] = {280, 180, 690, 300, 700, 470};                                                        // y coordinate of each city

  string dest[] = {"Phoenix"};                  //cities which been used in calculation, as a target city and starting city
  int dist[];                                    //calculated distanced between cities

  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
  int calc[];                                    //array to hold temporary distances
  int x1;
  int x2;
  int y1;
  int y2;

 public:
  Line();
  void calculate();
  void find();
  bool notInDest();
  void sort();
  void swap();
  void smallest();
  void display();
};

#endif;
I created prototypes of functions in cc program and tryed to compile, but got this error messages
line.h:10: syntax error before `['
line.h:10: missing ';' before right brace
line.h:14: syntax error before `['
line.h:24: parse error before `public'
line.h:33: parse error before `}'
line.cc:6: definition of implicitly-declared `Line::Line()'
line.cc:6: declaration of `Line::Line()' outside of class is not definition
line.cc:9: no `void Line::find()' member function declared in class `Line'
line.cc:12: parse error before `bool'
line.cc:16: parse error before `void'
line.cc:20: parse error before `void'
line.cc:24: parse error before `void'
line.h:15: storage size of `dist' isn't known
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?

Narue May 6th, 2005 8:00 pm
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.


All times are GMT -4. The time now is 5:11 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC