Returning a Struct from a Class

Hello, I need to return a Struct from a Class. The struct must contain two different string values, and a vector<string>. Here is the code I've written so far:

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
class interface {
public:
	typedef vector<string> sentence; //Sentence
	typedef vector<string> word; //Word from Sentence
	typedef vector<string> pos; //Part of Speech
	map<word, pos> INword; //Map of Single word with Parts of Speech(multiple)
	typedef vector<string> word_F; //Forms of "Word"
	map<word, word_F> OUTword; //Map of Single Word with multiple Forms of "Word"
	typedef string adr; //Addressee
	typedef vector<string> phrase; //Remaining Phrase (w/o Adr...)
	typedef string cmd;
	typedef map<string, string> command;
	

public:
		
		vector<string> input_sentence(vector<string>& words);

private:
	
		string det_adr(vector<string>& words)
	{
			string adr;
			adr=words.front();
			return adr;  //Returns to interface::input_sentence
	}
		string det_cmd(vector<string>& words)
	{
			words.erase(words.begin());
			vector<string>phrase;
			vector<string>::iterator it;
			string cmd;
			cmd=words.front();			
			return cmd; //Returns to interface::input_sentence
	}
		vector<string> det_phrase(vector<string>& words)
	{
		words.erase(words.begin());
		vector<string>::iterator it;
		return words; //Returns to interface::input_sentence
	}
};

vector<string> interface::input_sentence(vector<string>& words)
{
	string adr;
	adr = det_adr(words);
	string cmd;
	cmd = det_cmd(words);
	vector<string> phrase = det_phrase(words);
	//cout << infunc.adr << infunc.cmd << endl;
	return phrase; //THIS IS WHERE I NEED TO RETURN A STRUCT CONTAINING: string adr, cmd & vector<string>phrase
}

int main () {
	char sentence[200] ;
	cout << "ENTER SENTENCE: " << endl;
	cin.getline(sentence, sizeof(sentence));
	
	vector<string> words;
	words = sepWrds(sentence);
	
	interface test_class;
	vector<string>phrase;
	phrase = test_class.input_sentence(words);
	vector<string>::iterator it;
	for(it=phrase.begin();it<phrase.end();it++)
	{cout << *it << endl;}
	return 0;
}



I'm not sure how to initialize the struct...I get many errors. Any ideas?
Last edited on
A STRUCT CONTAINING: string adr, cmd & vector<string>phrase

If you want a struct containing those things and you want the function calling input_sentence to know of the struct, then you need to define the struct outside the function.

1
2
3
4
5
6
struct insertNameHere
{
     string adr;
     string cmd;
     vector<string> phrase;
};

then, you need to change the declaration of input_sentence and have it return your struct. Right now you have it returning a vector<string>.

insertNameHere interface::input_sentence(vector<string>& words);

then, you return it like so:

1
2
3
4
5
insertNameHere structToReturn;
structToReturn.adr det_adr(words);
structToReturn.cmd = det_cmd(words);
structToReturn.phrase = det_phrase(words);
return structToReturn;


Also, one source of your errors is you're returning a vector<string> called "phrase" but you have "phrase" as a typedef. This causes a name clash.
that alone doesn't work...the problem I'm having I think is declaring the function interface::input_sentence. The error from your method shacktar is: input_sentence undeclared...along with a bunch of other things that I believe are due to this one problem...

The call for input_sentence comes from main: test_class.input_sentence(words). How do I adjust this call to indicate a struct is to be returned...

Here is how I thought it should be done::

Input inmain = test_class.input_sentence(words);

Input is undeclared...so is inmain...so is input_sentence...

Any ideas?
This doesn't work?

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
struct Input
{
     string adr;
     string cmd;
     vector<string> phrase;
};

class interface {
...
public:
     Input input_sentence(vector<string>& words);
...
};

Input interface::input_sentence(vector<string>& words)
{
     Input toReturn;
     toReturn.adr = det_adr(words);
     toReturn.cmd = det_cmd(words);
     toReturn.phrase = det_phrase(words);

     return toReturn;
}

int main {
...
     interface test_class;
     Input inmain = test_class.input_sentence(words);
...
}


If not, please show your full code and the full compile errors you're receiving.
I needed to put the struct declaration above the class declaration...instead of above the function (input_sentence)...of course. thanks
It could also work if you put it above the function. You just need to use scope resolution.

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
class interface {
...
public:
     struct Input
     {
          string adr;
          string cmd;
          vector<string> phrase;
     };

     Input input_sentence(vector<string>& words);
...
};

interface::Input interface::input_sentence(vector<string>& words)
{
...
}

int main() {
...
     interface test_class;
     interface::Input inmain = test_class.input_sentence(words);
...
}
Last edited on
that's what I was trying to do...so, you can place the struct within the class, and call the function and struct together with:

interface::Input inmain = test_class.input_sentence(words);

thanks that makes sense
Topic archived. No new replies allowed.