Insertion Sort in a Class

Hello. I'm a Cpp newbie and I have a question that i can't find an answer to it. So Im writing this program about Tourist sights using a class. Inside i have name of the sight, addres and distance from the city center.I've done the input output, but i have some errors that I can't understand.
Can you guys tell me where am I mistaking and how to fix it?

Here is what i have done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 #include <iostream>
#include <string>
using namespace std;
class Tobekt {
  private: string name;
          string addres;
          double distance;
          int size;
         
  public:
  Tobekt_array();
  ~Tobekt_array();
    double GetDistance(void); 
    void InsertionSort(void);
    void input(void);
    void output(void);
}; 
Tobekt::Tobekt_array(){
     cout<<"Tourist landmarks:" ;
     cin>>size;
    Tobekt *A = new Tobekt[size];
}
Tobekt::~Tobekt_array()
{
	delete[] A;
}


double Tobekt::GetDistance(){
  return distance;    
}
void Tobekt::InsertionSort()
{
	for (int i = 1; i<size; i++)
	{
		Tobekt index = A[i];
		int dec = i;
while (dec>0 && A[dec - 1].GetDistance() >= index.GetDistance())
		{
			A[dec] = A[dec - 1];
			--dec;
		}
	}
}

void Tobekt::input(){
   
   cout<<"Landmark name: ";
   getline(cin, name);
   getline(cin, addres);
   cout<<"Addres: "; 
    getline(cin, addres);
    cout<<"Distance from the city center:  ";
    cin>>distance;
}
void Tobekt::output(){
    cout<<endl;
    cout<<"Landmark name: "<<name<<endl;
   cout<<"Addres: "<<addres<<endl; 
    cout<<"Distance from the city center: "<<distance<<endl;
    }


int main()
    { 
/*
    int n;
    cout<<"Tourist landmarks:" ;
    cin>>n;
    Tobekt *A = new Tobekt[n];
    for(int i=0; i<n; i++)
    {
        A[i].input();
    }
    
    for(int i=0; i<n; i++)
    {
        A[i].output();
    }
*/
   
return 0;
    }


and here are the errors:

main.cpp:19:16: error: ISO C++ forbids declaration of ‘Tobekt_array’ with no type [-fpermissive]
Tobekt_array();
^
main.cpp:20:16: error: expected class-name before ‘(’ token
~Tobekt_array();
^
main.cpp:26:22: error: ISO C++ forbids declaration of ‘Tobekt_array’ with no type [-fpermissive]
Tobekt::Tobekt_array(){
^
main.cpp:31:22: error: expected class-name before ‘(’ token
Tobekt::~Tobekt_array()
^
main.cpp:31:23: error: definition of implicitly-declared ‘Tobekt::~Tobekt()’
Tobekt::~Tobekt_array()
^
main.cpp: In member function ‘void Tobekt::InsertionSort()’:
main.cpp:44:18: error: ‘A’ was not declared in this scope
Tobekt index = A[i];
^
at a guess you changed your class name and forgot to fix it after:

class Tobekt {
private: string name;
string addres;
double distance;
int size;

public:
Tobekt_array(); //this looks like the constructor followed by destructor but the class name is different from the function name which is wrong, did you mean Tobekt() here?
Welcome to the forum, and thank you for an excellent first post! Many first timers don't use code tags or don't post errors, or don't post code, or don't post either!

Lines 11,12, 18, & 23: Tobekt_array should be Tobekt. The constructor and destructor must have the same name as the class.

I think you've gotten tripped up by the class that represents a single tourist site, and the class that represents the array of all the sites. You're doing all of this in one class and it would make more sense to use separate classes for each. Here are minimal class definitions that do this. I haven't hidden the data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Site {
public:
    string name;
    string addres;
    double distance;
    void input(void);
    void output(void);
};


class Tobekt
{
  private:
  public:
    int size;
    Site *A;
      Tobekt();
     ~Tobekt();
    void InsertionSort(void);
};

Thank you. That helped me realise my mistakes. So almost a week of typing code, starting all over again and again, now I did it with two classes and it compiled without any errors. Which was shocking. /You know what is the feeling when you have 0 errors from the first compiling/ But now it seems like i cant call the sort function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
using namespace std;

class Site {
private:
    string name;
    string addres;
    double distance;
public:
    double GetDistance();
    void input();
    void output();
};
class Tobekt
{
private:
    int size;
    Site *A;
public:
    void InsertionSort();
};

void Site::input(){

   cout<<"Landmark name: ";
   getline(cin, name);
   getline(cin, addres);
   cout<<"Addres: ";
    getline(cin, addres);
    cout<<"Distance from the city center:  ";
    cin>>distance;
}
void Site::output(){
    cout<<endl;
    cout<<"Landmark name: "<<name<<endl;
   cout<<"Addres: "<<addres<<endl;
    cout<<"Distance from the city center: "<<distance<<endl;
    }

double Site::GetDistance(){
  return distance;
}
void Tobekt::InsertionSort()
{
	for (int i = 1; i<size; i++)
	{
		Site index = A[i];
		int dec = i;
while (dec>0 && A[dec - 1].GetDistance() >= index.GetDistance())
		{
			A[dec] = A[dec - 1];
			--dec;
		}
	}
}

int main()
{
    int size;
    cout<<"Tourist landmarks:" ;
    cin>>size;
    Site *A = new Site[size];
    for(int i=0; i<size; i++)
    {
        A[i].input();

    }
    for(int i=0; i<size; i++)
    {
        A[i].InsertionSort();
        A[i].output();
    }
    delete []A;
return 0;
    }

In this situation it gives me an error error: 'class Site' has no member named 'InsertionSort'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    int size;
    cout<<"Tourist landmarks:" ;
    cin>>size;
    Site *A = new Site[size];
    for(int i=0; i<size; i++)
    {
        A[i].input();

    }
    for(int i=0; i<size; i++)
    {
        A[i].InsertionSort();
        A[i].output();
    }
    delete []A;
return 0;
    }

So my question is how to use this function a.k.a call it for the Array A?
Also when I write it Tobekt InsertionSort(); it declares it as a variable.
Topic archived. No new replies allowed.