I tried to make a linked list, but it doesn't work. Help me plz!

I want to declare string as protected in the header file, but I keep getting an error.

I want to declare a virtual function, but I get an error.

How can I solve it?


This is the parent class.

// Function.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class function {
	void fc();
public:
	int c;
	virtual void draw() {}
	virtual void count(int c) = 0;
public:
	void paint()
	{
		draw();
	}
	virtual ~function() {}
};



// Function.cpp
1
2
3
4
5
6
7
8
9
10
  #include <iostream>
#include <string>
#include "Function.h"
using namespace std;

void function::fc()
{
	cout << "1. Ingredients";
	cout << " 2. Foods" << endl << endl;	
}



This is inherited class 1.

// Ingredient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

class Ingredient : public function
{
	void ingre();

protected:
	std::string a, b;
	std::string name[6];
	virtual string ing(string a, string b) = 0;

	void count(int c);
	virtual void draw();

public:
	virtual ~Ingredient() {}
	void run();
};


// Ingredient.cpp

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
  #include <iostream>
#include <string>
#include "Function.h"
#include "Ingredient.h"

using namespace std;

string name[6] = { "Apple", "Egg", "Ham", "Potato", "Cheese", "Tuna" };

void Ingredient::ingre()
{
	cout << "Ingredient : ";
}

void Ingredient::count(int c)
{
	run();
	cout << endl << "Enter 2 ingredients you want : ";
	cin >> a >> b;
}

void Ingredient::draw()
{
	count(c);
	cout << endl;
	ing(a, b);
}

void Ingredient::run()
{
	ingre();
	for (int i = 0; i < 6; i++)
		cout << name[i] << " ";
	cout << endl;
}


This is inherited class 2.

// Food.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>

class Food : public function
{
	void comb();

protected:
	std::string a;
	std::string names[15];
	virtual string ele(string a) = 0;
	virtual void draw();
	virtual void count(int c);

public:
	void Set();
};



// Food.cpp
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
#include <iostream>
#include <string>
#include "Function.h"
#include "Food.h"

using namespace std;

string names[15] = { "Aaa", "Bbb", "Ccc", "Ddd", "Eee", "Fff", "Ggg", "Hhh", "Iii", "Jjj", "Kkk", "Lll", "Mmm", "Nnn", "Ooo" };

void Food::comb()
{
	cout << "Foods : ";
}

void Food::draw()
{
	cout << endl;
	count(c);
	cout << endl;
}

void Food::count(int c)
{
	Set();
	cout << endl << "Enter the food you want : ";
	cin >> a;
	ele(a);
}

void Food::Set()
{
	comb();
	for (int i = 0; i < 5; i++)
		cout << names[i] << " ";
	cout << endl;
	cout << "         ";
	for (int i = 5; i < 10; i++)
		cout << names[i] << " ";
	cout << endl;
	cout << "         ";
	for (int i = 10; i < 15; i++)
		cout << names[i] << " ";
	cout << endl;
}


This is a class that inherits from inherited class1.

// Com.h

1
2
3
4
5
6
7
8
9
10
#pragma once
#include <string>

class Com :public Ingredient {
	std::string names[15];
protected:
	string ing(string a, string b);
public:
	virtual ~Com() {}
};



// Com.cpp

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
#include <iostream>
#include <string>
#include "Function.h"
#include "Ingredient.h"
#include "Com.h"

using namespace std;

string names[15] = { "Aaa", "Bbb", "Ccc", "Ddd", "Eee", "Fff", "Ggg", "Hhh", "Iii", "Jjj", "Kkk", "Lll", "Mmm", "Nnn", "Ooo" };

string Com::ing(string a, string b)
{
	if (a == name[0] && b == name[1] || a == name[1] && b == name[0])
		cout << names[0] << endl;
	else if (a == name[0] && b == name[2] || a == name[2] && b == name[0])
		cout << names[1] << endl;
	else if (a == name[0] && b == name[3] || a == name[3] && b == name[0])
		cout << names[2] << endl;
	else if (a == name[0] && b == name[4] || a == name[4] && b == name[0])
		cout << names[3] << endl;
	else if (a == name[0] && b == name[5] || a == name[5] && b == name[0])
		cout << names[4] << endl;
	else if (a == name[1] && b == name[2] || a == name[2] && b == name[1])
		cout << names[5] << endl;
	else if (a == name[1] && b == name[3] || a == name[3] && b == name[1])
		cout << names[6] << endl;
	else if (a == name[1] && b == name[4] || a == name[4] && b == name[1])
		cout << names[7] << endl;
	else if (a == name[1] && b == name[5] || a == name[5] && b == name[1])
		cout << names[8] << endl;
	else if (a == name[2] && b == name[3] || a == name[3] && b == name[2])
		cout << names[9] << endl;
	else if (a == name[2] && b == name[4] || a == name[4] && b == name[2])
		cout << names[10] << endl;
	else if (a == name[2] && b == name[5] || a == name[5] && b == name[2])
		cout << names[11] << endl;
	else if (a == name[3] && b == name[4] || a == name[4] && b == name[3])
		cout << names[12] << endl;
	else if (a == name[3] && b == name[5] || a == name[5] && b == name[3])
		cout << names[13] << endl;
	else if (a == name[4] && b == name[5] || a == name[5] && b == name[4])
		cout << names[14] << endl;
	else {
		cout << endl << "The ingredients you entered are not in the Ingredients menu. Please try again." << endl << endl;
		cout << "Please enter two ingredients. : " << endl;
		cin >> a >> b;
		ing(a, b);
	}
	return 0;
}


This is a class that inherits from inherited class2.

// Material.h
1
2
3
4
5
6
7
8
9
10
#include <string>

class Material : public Food
{
protected:
	std::string name[6];
public:
	string ele(string a);
	virtual ~Material() {}
};



// Material.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include "Function.h"
#include "Food.h"
#include "Material.h"

using namespace std;

string name[6] = { "Apple", "Egg", "Ham", "Potato", "Cheese", "Tuna" };

string Material::ele(string a)
{
	if (a == names[0])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[0] << ", " << name[1] << endl;
	}
	else if (a == names[1])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[0] << ", " << name[2] << endl;
	}
	else if (a == names[2])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[0] << ", " << name[3] << endl;
	}
	else if (a == names[3])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[0] << ", " << name[4] << endl;
	}
	else if (a == names[4])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[0] << ", " << name[5] << endl;
	}
	else if (a == names[5])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[1] << ", " << name[2] << endl;
	}
	else if (a == names[6])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[1] << ", " << name[3] << endl;
	}
	else if (a == names[7])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[1] << ", " << name[4] << endl;
	}
	else if (a == names[8])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[1] << ", " << name[5] << endl;
	}
	else if (a == names[9])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[2] << ", " << name[3] << endl;
	}
	else if (a == names[10])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[2] << ", " << name[4] << endl;
	}
	else if (a == names[11])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[2] << ", " << name[5] << endl;
	}
	else if (a == names[12])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[3] << ", " << name[4] << endl;
	}
	else if (a == names[13])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[3] << ", " << name[5] << endl;
	}
	else if (a == names[14])
	{
		cout << endl << "Ingredients that make up food : ";
		cout << name[4] << ", " << name[5] << endl;
	}
	else {
		cout << endl << "The food you entered is not in the food menu. Please try again." << endl << endl;
		cout << "Enter the food you want : " << endl;
		cin >> a;
		ele(a);
	}

	return 0;
}



The Last Code
// Main.cpp
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
#include <iostream>
#include <string>

#include "Function.h"
#include "Ingredient.h"
#include "Com.h"

#include "Food.h"
#include "Material.h"

using namespace std;

int main()
{
	cout << endl << endl << "----------Simple Cooking TOOL----------" << endl << endl << endl;
	function* rec;
	rec = new Com;
	function* cir;
	cir = new Material;

	cout << "Enter the number of the desired function : " << endl;
	cout << "(1: ingredients to food, 2: food to ingredients)" << endl;

	int c;
	cin >> c;

	if (c == 1) {
		rec->paint();
	}
	else if (c == 2) {
		cir->paint();
	}
	else exit;

	delete rec;
	delete cir;

	return 0;
}


Thank you very much for watching this very long article.
I want you always full of luck and joy.
Last edited on
I assume that you want getting access of your std::string arrays within your objectts. Maybe is this what fit your needs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>

class Ingredient : public function
{
	void ingre();

protected:
	std::string a, b;
	std::string * name;
	virtual std::string ing(std::string a, std::string b) = 0; // std was missed

	void count(int c);
	virtual void draw();

public:
	Ingredient();
	virtual ~Ingredient() {}
	void run();
};


1
2
3
4
5
6
std::string x_name[6] = { "Apple", "Egg", "Ham", "Potato", "Cheese", "Tuna" };

Ingredient::Ingredient()
{
    name = x_name;
}

Same with std::string names[15].

Beside this, your code is a good example why it's not good using namespace std; because it seems that you become lost of where you need std:: and where not.

*edit* syntax
Last edited on
Thanks nuderobmonkey!
Thanks to that, I realized that using namespace std; is more important!
I wish you good luck all day tomorrow!
Topic archived. No new replies allowed.