Variable Not Defined

Started by
14 comments, last by Brain 7 years, 3 months ago

hi guys.

Ive created a Config file where the program will getting the values for the variables, but when i "register" a variable in the config.h and config.cpp and including the config.h in the main.cpp , im getting error variable not defined. The same error is happening for every variable in my configs.

Config.h



#pragma once

#include <string>
#include <set>
#include <vector>
#include <windows.h>
class Config {
public:
	static Config* Instance();
	void Reload();

	struct Server {
		void Load(Config *config);
		int serverport;
		std::wstring serveraddr;
	
	/*	time_t shutdownDuration; [example]
	bool globalShout; [example]
	std::vector<INT64> vitalityLevels; [example]
	INT32 fixedPCCafePoints; [example] */
	} *server;


protected:
	Config(const wchar_t *filename);
	std::wstring GetString(const wchar_t *section, const wchar_t *name, const wchar_t *defaultValue);
	INT64 GetInt(const wchar_t *section, const wchar_t *name, const INT64 defaultValue);
	bool GetBool(const wchar_t *section, const wchar_t *name, const bool defaultValue);
	double GetDouble(const wchar_t *section, const wchar_t *name, const double defaultValue);
	std::vector<INT64> GetIntList(const wchar_t *section, const wchar_t *name, const std::vector<INT64> &defaultValue);
	std::set<INT64> GetIntSet(const wchar_t *section, const wchar_t *name, const std::set<INT64> &defaultValue);

	std::wstring filename;
	static Config *instance;
};


Config.cpp:


#include "Config.h"
#include <windows.h>
#include <fstream>
#include <sstream>

Config *Config::instance = 0;

Config::Config(const wchar_t *filename) :
	filename(filename),
	server(new Server)
{
	Reload();
}

void Config::Reload()
{
	server->Load(this);
}

void Config::Server::Load(Config *config)
{
	serverport = config->GetInt(L"server", L"ServerPort", 1111);
	serveraddr = config->GetString(L"server", L"ServerAddr", L"127.0.0.1");

	/* [Examples]
	globalShout = config->GetBool(L"server", L"GlobalShout", false);
	vitalityLevels = config->GetIntList(L"server", L"VitalityLevels", std::vector<INT64>());
	*/
}

//should be changed
Config* Config::Instance()
{
	const static char *paths[] = {
		".\\Config.ini",
		"..\\Config.ini",
		"..\\l2server\\Config.ini",
		"..\\server\\Config.ini",
		"Config.ini",
		0 };

	
	if (instance) {
		return instance;
	}
	for (size_t i(0); paths[i]; ++i) {
		if (std::ifstream(paths[i])) {	
			return instance;
		}
	}
	MessageBox(0,
		L"Can't find suitable config Config.ini.\r\n\r\n"
		L"To use global config for all daemons, place it in top level directory \r\n"
		L"To use local configs for single daemons, place it to daemon directory (Cached, NPC, L2Server)", L"ERROR", 0);
	exit(1);
}
	
	

std::wstring Config::GetString(const wchar_t *section, const wchar_t *name, const wchar_t *defaultValue)
{
	static wchar_t buffer[16384];
	GetPrivateProfileString(section, name, defaultValue, buffer, sizeof(buffer), filename.c_str());
	return std::wstring(buffer);
}

INT64 Config::GetInt(const wchar_t *section, const wchar_t *name, const INT64 defaultValue)
{
	INT64 ret;
	std::wstring s;
	{
		std::wstringstream ss;
		ss << defaultValue;
		s = GetString(section, name, ss.str().c_str());
	}
	std::wstringstream ss;
	ss << s;
	ss >> ret;
	if (ss.fail()) {
		ret = defaultValue;
	}
	return ret;
}

bool Config::GetBool(const wchar_t *section, const wchar_t *name, const bool defaultValue)
{
	std::wstring s = GetString(section, name, L"");
	if (s == L"1" || s == L"true" || s == L"on" || s == L"yes") {
		return true;
	}
	else if (s == L"0" || s == L"false" || s == L"off" || s == L"no") {
		return false;
	}
	else {
		return defaultValue;
	}
}

double Config::GetDouble(const wchar_t *section, const wchar_t *name, const double defaultValue)
{
	double ret;
	std::wstring s;
	{
		std::wstringstream ss;
		ss << defaultValue;
		s = GetString(section, name, ss.str().c_str());
	}
	std::wstringstream ss;
	ss << s;
	ss >> ret;
	if (ss.fail()) {
		ret = defaultValue;
	}
	return ret;
}

std::vector<INT64> Config::GetIntList(const wchar_t *section, const wchar_t *name, const std::vector<INT64> &defaultValue)
{
	std::wstring s(GetString(section, name, L""));
	if (s.empty()) {
		return defaultValue;
	}
	std::vector<INT64> result;
	if (s == L"-" || s == L"null") {
		return result;
	}
	std::wstring part;
	for (size_t i(0); i < s.size(); ++i) {
		if (s[i] == L' ' || s[i] == L',' || s[i] == L';') {
			if (!part.empty()) {
				std::wstringstream ss;
				INT64 i;
				ss << part;
				ss >> i;
				result.push_back(i);
				part.clear();
			}
		}
		else if (s[i] >= L'0' && s[i] <= L'9') {
			part.push_back(s[i]);
		}
	}
	if (!part.empty()) {
		std::wstringstream ss;
		INT64 i;
		ss << part;
		ss >> i;
		result.push_back(i);
	}
	return result;
}

std::set<INT64> Config::GetIntSet(const wchar_t *section, const wchar_t *name, const std::set<INT64> &defaultValue)
{
	std::wstring s(GetString(section, name, L""));
	if (s.empty()) {
		return defaultValue;
	}
	std::set<INT64> result;
	if (s == L"-" || s == L"null") {
		return result;
	}
	std::wstring part;
	for (size_t i(0); i < s.size(); ++i) {
		if (s[i] == L' ' || s[i] == L',' || s[i] == L';') {
			if (!part.empty()) {
				std::wstringstream ss;
				INT64 i;
				ss << part;
				ss >> i;
				result.insert(i);
				part.clear();
			}
		}
		else if (s[i] >= L'0' && s[i] <= L'9') {
			part.push_back(s[i]);
		}
	}
	if (!part.empty()) {
		std::wstringstream ss;
		INT64 i;
		ss << part;
		ss >> i;
		result.insert(i);
	}
	return result;
}


Main.cpp:


#include "Server.h"
#include "database.h"
#include "ScriptLoader.h"
#include "Config.h"


int main()
{
	system("color 5a");
	std::cout << "Loading MySQL Database" << std::endl;
	Sleep(300);
    MySQL:MySQL();
	std::cout << "Loading Scripts" << std::endl;
	Sleep(300);
   	ScriptLoad();
	std::cout << "Server Has Started" << std::endl;
	Server MyServer(serverport); //Create server on port 1111
	for (int i = 0; i < 100; i++) // Max Players Online (100)
	{
		MyServer.ListenForNewConnection(); //Accept new connection (if someones trying to connect)
	}
	EngineDelete();
	system("pause");
	return 0;
}
Advertisement

Do you have a config file with valid values to load?

Have you confirmed your ScriptLoad actually works? (Set a breakpoint after loading, and check the data structures that should contain the loaded data. Alternatively, write a quick and dirty dump routine or save the config to a different file.)

If this "error variable not defined" an error of your own, or from the compiler? I don't see such an error message in your code, but the supplied code is not complete.

If it is from the compiler, where exactly in the code is that? On what variable?

If it is your own error, have you established at what point in the loading things go wrong exactly? The best tool here is a debugger, stepping through the code, and checking that the results are as you expect. Alternatively, add more error messages that vary depending on the precise test that fails. That should give you more information where things don't work as expected.

im getting error for this: serverport

but i use #include "Config.h"

im getting error for this: serverport
but i use #include "Config.h"


Paste the actual error in its entirety: all file names, line numbers, and supplemental text. Do not paraphrase; copy-n-paste straight from your compiler's output.

These errors are long and exacting for a reason. They contain vital information.

Sean Middleditch – Game Systems Engineer – Join my team!

There is no variable named "serverport" there, Config.h only contains a "class Config"

Maybe you mean


Config *cfg = Config::Instance();
cfg->server->serverport

except "cfg" is always "0", as you never seem to assign a Config instance to the "instance" variable.

Having this kind of singleton variables doesn't do much good in general, it's much better to make a Config variable somewhere in the main program, and either add the Server structure in the Config object (which makes Reload much nicer), or also have a Config::Server variable in the main program.

It eliminates the Config::instance and Config::server variables, and makes explicit dependencies for code that uses that data.

changed main.cpp a little bit to:


#include "Server.h"
#include "database.h"
#include "ScriptLoader.h"
#include "Config.h"


int main()
{
	system("color 5a");
	Config::Instance();
	std::cout << "Config File Loaded" << std::endl;
	std::cout << " " << std::endl;
	std::cout << " " << std::endl;
	std::cout << "Loading MySQL Database" << std::endl;
	Sleep(300);
    MySQL:MySQL();
	std::cout << "Loading Scripts" << std::endl;
	Sleep(300);
   	ScriptLoad();
	std::cout << " " << std::endl;
	std::cout << " " << std::endl;
	std::cout << "Server Has Started" << std::endl;
	Server MyServer(serverport); //Create server on port 1111
	for (int i = 0; i < 100; i++) // Max Players Online (100)
	{
		MyServer.ListenForNewConnection(); //Accept new connection (if someones trying to connect)
	}
	EngineDelete();
	system("pause");
	return 0;
}

The error is the same:

Severity Code Description Project File Line Suppression State
Error C2065 'serverport': undeclared identifier serverside c:\users\alex_\desktop\123\serverside\serverside\main.cpp 23

Severity Code Description Project File Line Suppression State
Error (active) identifier "serverport" is undefined serverside c:\Users\alex_\Desktop\123\serverside\serverside\Main.cpp 23

Where do you think you're defining ::serverport?

Stephen M. Webb
Professional Free Software Developer

struct Server {
void Load(Config *config);
int serverport;

That is not a variable, that is a structure definition, a description of the entries that will exist if you create space in memory for an instance of a Server.

that is, you need one of


Server s;
Server *p = new Server;

"s" is a variable of type Server, and it has a "s.serverport" containing a number.

if i use this stracture :

Config *cfg = Config::Instance();
cfg->server->serverport;

main.cpp:


#include "Server.h"
#include "database.h"
#include "ScriptLoader.h"
#include "Config.h"


int main()
{
	system("color 5a");
	Config *cfg = Config::Instance();
	cfg->server->serverport;
	std::cout << "Config File Loaded" << std::endl;
	std::cout << "Loading MySQL Database" << std::endl;
	Sleep(300);
    MySQL:MySQL();
	std::cout << "Loading Scripts" << std::endl;
	Sleep(300);
   	ScriptLoad();
	std::cout << "Server Has Started" << std::endl;
	Server MyServer(serverport); //Create server on port 1111
	for (int i = 0; i < 100; i++) // Max Players Online (100)
	{
		MyServer.ListenForNewConnection(); //Accept new connection (if someones trying to connect)
	}
	EngineDelete();
	system("pause");
	return 0;
}

im getting the same error

This topic is closed to new replies.

Advertisement