[cpp] Problem with load items and equip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Log:
main.cpp: In function 'int main()':
main.cpp:6:8: error: no matching function for call to 'CItem::CItem()'
  CItem Items;
        ^

main.cpp:6:8: note: candidates are:
In file included from Character.hpp:3:0,
                 from main.cpp:2:
Item.hpp:12:3: note: CItem::CItem(std::string, int, int, int)
   CItem(string name, int atackMin, int atackMax, int defence);
   ^
Item.hpp:12:3: note:   candidate expects 4 arguments, 0 provided
Item.hpp:4:7: note: CItem::CItem(const CItem&)
 class CItem
       ^
Item.hpp:4:7: note:   candidate expects 1 argument, 0 provided

C:\Users\User\Desktop\Baza\Makefile.win:28: recipe for target 'main.o' failed
mingw32-make.exe: *** [main.o] Error 1

Compilation failed after 0.72 seconds with errors

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
//main.cpp
#include "main.hpp"
#include "Character.hpp"
int main()
{
	CCharacter Player;
	CItem Items;
	Items.loadItems();
	Player.equipItems();
	cout<<Player.p_rightHand->name;
	return 0;
}
//main.hpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
//Character.hpp
#ifndef Character_hpp
#define Character_hpp
#include "Item.hpp"
class CCharacter
{
	public:
		string name;
		int attackMin;
		int attackMax;
		int defence;
		vector <CItem*> backPack;
		CItem *p_rightHand;
		int equipItems();
	
};
#endif
//Character.cpp
#include "Character.hpp"
CCharacter::equipItems()
{
	p_rightHand = CItem.AllItems[1];
}
//Item.hpp
#ifndef Item_hpp
#define Item_hpp
#include "main.hpp"
class CItem
{
	public:
		string name;
		int atackMin;
		int atackMax;
		int defence;
		vector <CItem*> AllItems;
		CItem(string name, int atackMin, int atackMax, int defence);
		int loadItems();
	
};
#endif
//Item.cpp
#include "Item.hpp"
CItem::CItem( string name_, int atackMin_, int atackMax_, int defence_ )
{
	name = name_;
	atackMin = atackMin_;
	atackMax = atackMax_;
	defence = defence_;
}
int CItem::loadItems()
{
	fstream itemBase;
	itemBase.open( "items.txt", ios::in );
	 for( int i = 0; i < 5; ++i )
	{
		itemBase >> name >> atackMin >> atackMax >> defence;
		AllItems.push_back(new CItem(name, atackMin,atackMax,defence));
	}
	 for( int i = 0; i < 5; ++i )
	{
        cout<<Allitems[i]->name<<endl;
	}
}


1
2
3
4
5
6
//items.txt
Sword 1 3 0
Dagger 2 2 0
Shield 0 0 1
Shirt 0 0 2
Helmet 0 0 1
Last edited on
Your error tels you that you have not defined a constructor with no parameters. You have two options:

1. Define CItem(); in class CItem, something that does the same thing as CItem(string name, int atackMin, int atackMax, int defence);, but without requiring a name, an atackMin, and atacMax, and defence. The implementation would be easy, just assign name="", atackmin=0, and so on, then call the constructor with those values.
2. In main, line 7, use CItem Items("",0,0,0); or something that makes sense

P.S. You can also define a constructor that does nothing
Last edited on
Ok program not compile. :(
error:
1
2
3
4
Character.cpp: In member function 'int CCharacter::equipItems()':
Character.cpp:5:16: error: expected primary-expression before '.' token
  p_rightHand = .AllItems[1];
  

when i try this:
1
2
3
4
5
6
7
//character.cpp
#include "Character.hpp"
int CCharacter::equipItems()
{
	CItem Item("",0,0,0);
	p_rightHand = .AllItems[1];
}

program be compiled but stop working in console. How fix it?
Topic archived. No new replies allowed.