In C + +, other functions cannot be defined in main function!

Posted by teddirez on Sun, 17 Nov 2019 17:30:09 +0100

VS2017, C + + language.
There is a strange problem in programming today. The program is as follows:

#include<iostream>
#include<string>
#include <vector>
using namespace std;
int main()
{
	struct conf
	{
		char itemname[40];
		char itemcontent[100];
	};

	char * GetInfo(vector<conf *> & conflist, const char *pitem)
	{
		for (auto pos = conflist.begin(); pos != conflist.end(); ++pos)
		{
			if (_stricmp((*pos)->itemname, pitem) == 0)
				return (*pos)->itemcontent;
		}
		return nullptr;
	}
	//Servername = zone 1;
//ServerID = 100000;
	conf *pconf1 = new conf;
	strcpy_s( pconf1->itemname, sizeof(pconf1->itemname), "ServerName");
	strcpy_s( pconf1->itemcontent, sizeof(pconf1->itemcontent), "1 area");

	conf *pconf2 = new conf;
	strcpy_s(pconf2->itemname, sizeof(pconf2->itemname), "ServerID");
	strcpy_s(pconf2->itemcontent, sizeof(pconf2->itemcontent), "100000");

	vector <conf*>conflist;
	conflist.push_back(pconf1);
	conflist.push_back(pconf2);

	char * p_tmp = GetInfo (conflist, "ServerName");
	if (p_tmp != nullptr)
	{
		cout << p_tmp << endl;
	}
	//We need to release memory, and new needs to release correspondingly, otherwise it will cause memory leakage
	vector<conf*>::iterator pos;
	for (pos = conflist.begin() ;pos != conflist.end(); ++pos)
	{
		delete (*pos);//*pos is the pointer / / it must be released by itself
	}
	conflist.clear();//Do you want this or not? It will be recycled at the end
	system("pause");
	return 0;
}

When compiling, the following errors are prompted:

1 > e: \ C + + \ temp \ temp \ x0503.cpp (18): error C2601: "GetInfo": local function definition is illegal
1 > e: \ C + + \ temp \ temp \ x0503.cpp (9): Note: there is a "{" in this line without a match
1 > finished building project "temp.vcxproj" - failed.
==========Build: 0 succeeded, 1 failed, 0 latest, 0 skipped==========
After careful inspection for many times, no problem was found. Later, it was found that GetInfo should not be defined in the main function, that is, C + + does not support the nested definition of the function, but the nested call of the function is OK!
So the correct code is as follows:

#include<iostream>
#include<string>
#include <vector>
using namespace std;

struct conf
{
	char itemname[40];
	char itemcontent[100];
};

char * GetInfo(vector<conf *> & conflist, const char *pitem)
{
	for (auto pos = conflist.begin(); pos != conflist.end(); ++pos)
	{
		if (_stricmp((*pos)->itemname, pitem) == 0)
			return (*pos)->itemcontent;
	}
	return nullptr;
}

int main()
{
	
	
	//Servername = zone 1;
//ServerID = 100000;
	conf *pconf1 = new conf;
	strcpy_s( pconf1->itemname, sizeof(pconf1->itemname), "ServerName");
	strcpy_s( pconf1->itemcontent, sizeof(pconf1->itemcontent), "1 area");

	conf *pconf2 = new conf;
	strcpy_s(pconf2->itemname, sizeof(pconf2->itemname), "ServerID");
	strcpy_s(pconf2->itemcontent, sizeof(pconf2->itemcontent), "100000");

	vector <conf*>conflist;
	conflist.push_back(pconf1);
	conflist.push_back(pconf2);

	char * p_tmp = GetInfo (conflist, "ServerName");
	if (p_tmp != nullptr)
	{
		cout << p_tmp << endl;
	}

	//We need to release memory, and new needs to release correspondingly, otherwise it will cause memory leakage

	vector<conf*>::iterator pos;
	for (pos = conflist.begin() ;pos != conflist.end(); ++pos)
	{
		delete (*pos);//*pos is the pointer / / it must be released by itself
	}

	conflist.clear();//Do you want this or not? It will be recycled at the end




	system("pause");
	return 0;
}

Output result: Zone 1

Topics: Programming