Vector with my own class not working

I have a settings class and a settingItem class. The settings class has a vector of settingItems. The vector is not working:
1
2
error C2065: 'settingItem' : undeclared identifier
 error C2923: 'std::vector' : 'settingItem' is not a valid template type argument for parameter '_Ty'


with code line:

vector<settingItem> settings;


Help?
Read your code from left to right, top to bottom.
At the point that you declare `vector<settingItem> settings;' ¿do you know that `settingItem' exists?

Later, ¿do you know what `settingItem' is?
Where is settingItem defined? If it's in a header, do you include that header? If the code you posted is also in a header, is that header included by the settingItem header? (if so, that's a problem)
Everything works but this... Anyway I will post all files.

Settings.h:
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
#ifndef SETTINGS_H
#define SETTINGS_H


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "SettingItem.h"

using namespace std;


	class Settings
	{
	public:
		enum StrType{Keyword = 1, Description = 2, Data = 3};
		Settings(std::string filename);
		fstream& getFile();
		void deleteFile();
		void addSetting(std::string keyword, std::string description = "",std::string data = "");
		void deleteSetting(std::string keyword);
		void deleteSetting(int iter);
		void write();
		void openFile(std::string filename);
		void setSettingData(StrType type, int iter, std::string data);
		void setSettingData(StrType type, string keyword, std::string data);
		std::string getSettingData(StrType type, int iter);
		std::string getSettingData(StrType type, std::string keyword);
		int getSettingSize();
		bool getState();
		void loadSettings();
	private:
		int findSetting(std::string keyword);
		fstream file;
		std::vector<settingItem> settings;
		std::string filename;
		bool state;
	};


#endif //SETTINGS_H 


Settings.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Settings.h"

using namespace std;


	Settings::Settings(string filename)
	{
		file.open(filename);
		if(file.is_open())
			state = true;
		else
			state = false;
	}

	inline fstream& Settings::getFile(){return file;}

	inline void Settings::deleteFile(){remove(filename.c_str());}

	inline void Settings::addSetting(string keyword, string description, string data){settings.push_back(settingItem(keyword, description, data));}

	inline void Settings::deleteSetting(int iter){settings.erase((settings.begin() + iter));}

	inline void Settings::deleteSetting(string keyword){settings.erase((settings.begin() + findSetting(keyword)));}

	void Settings::write()
	{
		if(filename == "")
		{
			return;
		}
		file.open(filename);
		if(file.is_open())
		{
			file << "--Settings--\n\n";
			for(unsigned int i = 0; i < settings.size(); i++)
			{
				file << endl << "#"<<settings[i].getKeyword() << "#" << "("<<settings[i].getDescription() <<")"<<" = " << "[" << settings[i].getData()<< "]\n";
			}
		}
	}

	inline void Settings::openFile(string filename){file.open(filename);}

	void Settings::setSettingData(StrType type, int iter, string data)
	{
		if(type == Data)
			settings[iter].setData(data);	
		else if(type == Description)
			settings[iter].setDescription(data);
		else if(type == Keyword)
			settings[iter].setKeyword(data);
	}

	void Settings::setSettingData(StrType type, string keyword, string data)
	{
			if(type == Data)
			settings[findSetting(keyword)].setData(data);	
		else if(type == Description)
			settings[findSetting(keyword)].setDescription(data);
		else if(type == Keyword)
			settings[findSetting(keyword)].setKeyword(data);
	}

	string Settings::getSettingData(StrType type, int iter)
	{
		if(type == Keyword)
			return settings[iter].getKeyword();		
		else if(type == Description)
			return settings[iter].getDescription();
		else if(type == Data)
			return settings[iter].getData();

		return "";
	}

	string Settings::getSettingData(StrType type, string keyword)
	{
		if(type == Keyword)
			return settings[findSetting(keyword)].getKeyword();		
		else if(type == Description)
			return settings[findSetting(keyword)].getDescription();
		else if(type == Data)
			return settings[findSetting(keyword)].getData();

		return "";
	}

	inline int Settings::getSettingSize(){return settings.size();}

	inline bool Settings::getState(){return state;}

	void Settings::loadSettings()
	{
		if(getState())
		{
			bool found = false;
			string temp = "";
			for(int i = 0; i < getSettingSize(); i++)
			{
				while(!found)
				{
					file.ignore( numeric_limits<streamsize>::max(),'#');
					getline(file, temp, '#');
					if(temp == settings[i].getKeyword())
					{
						file.ignore( numeric_limits<streamsize>::max(),'(');
						getline(file, temp, ')');
						settings[i].setDescription(temp);

						file.ignore( numeric_limits<streamsize>::max(),'[');
						getline(file, temp, ']');
						settings[i].setData(temp);
						found = true;
					}
					if(file.eof())
					{
						file.seekg(0);
					}
				}
			}
			
		}
		else
			return;
	}

	int Settings::findSetting(string keyword)
	{
		for(unsigned int i = 0; i < settings.size(); i++)
		{
			if(settings[i].getKeyword() == keyword)
				return i;
		}
		return 0;
	}


SettingItem.h:
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
#ifndef SETTINGITEM_H
#define SETTINGITEM_H

#ifndef SETTINGS_H
#include "Settings.h"
#endif //SETTINGS_H


	class settingItem
	{
	public:
		//Initializes all data members to ""
		settingItem();
		//Initializes all data members to the data specified
		settingItem(std::string keyword, std::string description, std::string data); 
		//Initializes keyword and data to the data specified
		settingItem(std::string keyword, std::string data); 
		//Initializes keyword to the data specified
		settingItem(std::string keyword); 
		std::string getKeyword();
		std::string getDescription();
		std::string getData();
		void setAllStringItems(std::string keyword, std::string description, std::string data);
		void setKeyword(std::string keyword);
		void setDescription(std::string description);
		void setData(std::string data);
		void clearAll();
		void clearKeyword();
		void clearDescription();
		void clearData();
		std::string getAllStringItems();
	private:
		std::string theKeyword;
		std::string theDescription;
		std::string theData;

	};



#endif //SETTINGITME_H 


SettingItem.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
#include "SettingItem.h"

using namespace std;


settingItem::settingItem() 
{
	theData = "";
	theDescription = "";
	theKeyword = "";
	return;
}

settingItem::settingItem(string keyword,string description,string data): theKeyword(keyword), theDescription(description), theData(data){}

settingItem::settingItem(string keyword, string data): theKeyword(keyword), theData(data){}

settingItem::settingItem(string keyword): theKeyword(keyword){}

inline string settingItem::getKeyword() {return theKeyword;}

inline string settingItem::getDescription() {return theDescription;}

inline string settingItem::getData() {return theData;}

inline string settingItem::getAllStringItems() {return (theKeyword + theDescription + theData);}

inline void settingItem::setData(string data) {theData = data;}

inline void settingItem::setDescription(string description) {theDescription = description;}

inline void settingItem::setKeyword(string keyword) {theKeyword = keyword;}

inline void settingItem::setAllStringItems(string keyword, string description, string data){theKeyword = keyword; theDescription = description; theData = data;}

inline void settingItem::clearAll(){theKeyword.clear();theDescription.clear();theData.clear();}

inline void settingItem::clearKeyword(){theKeyword.clear();}

inline void settingItem::clearDescription(){theDescription.clear();}

inline void settingItem::clearData(){theData.clear();}


Main is just return 0. I am getting compile errors from this line int Settings.h:
std::vector<settingItem> settings;

I have no idea why. Sorry for long code.
Hey L B,

I was including "Settings.h" in "settingItem.h" and it fixed the error! thank you!
New prob.

Link Errors. Oh Great.

Here they are:

1
2
3
4
5
6
7
Error	4	error LNK2019: unresolved external symbol "public: void __thiscall settingItem::setKeyword(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setKeyword@settingItem@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall Settings::setSettingData(enum Settings::StrType,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setSettingData@Settings@@QAEXW4StrType@1@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	
Error	5	error LNK2019: unresolved external symbol "public: void __thiscall settingItem::setDescription(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setDescription@settingItem@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall Settings::loadSettings(void)" (?loadSettings@Settings@@QAEXXZ)	
Error	6	error LNK2019: unresolved external symbol "public: void __thiscall settingItem::setData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setData@settingItem@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall Settings::loadSettings(void)" (?loadSettings@Settings@@QAEXXZ)	
Error	1	error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall settingItem::getKeyword(void)" (?getKeyword@settingItem@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "private: int __thiscall Settings::findSetting(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?findSetting@Settings@@AAEHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	
Error	2	error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall settingItem::getDescription(void)" (?getDescription@settingItem@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Settings::getSettingData(enum Settings::StrType,int)" (?getSettingData@Settings@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@W4StrType@1@H@Z)	
Error	3	error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall settingItem::getData(void)" (?getData@settingItem@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Settings::getSettingData(enum Settings::StrType,int)" (?getSettingData@Settings@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@W4StrType@1@H@Z)	
Error	7	error LNK1120: 6 unresolved externals	


As I already posted the code, you know that I defined all of these functions mentioned. I have no clue.
Last edited on
:-\

But you defined them as inline, so they'll only be usable in the files where the definitions are visible. Which is just SettingItem.cpp at the moment.

Either move all the inline methods from SettingItem.cpp to SettingItem.h (after the class but before the #endif)

Or remove inline from in front of your methods

Andy
Last edited on
Oh. Ok. I didn't know that. I will just take 'inline' out.

Thanks!

It works! Apparently, the reason that those were the only ones that failed was because they were the only ones that were used.
The linker cares not for that which is unused.
Topic archived. No new replies allowed.