Nooby C++ Question :(
by Timothy Volpe · in Torque Game Engine Advanced · 08/08/2010 (3:38 pm) · 8 replies
Please forgive me for asking this question, but I've spent an hour making a script, and another hour trying to fix some issues. This is also towards learning, because once I figure out why this is broken, I can fix it on my own later.
So I wrote a small script for a test. This script isn't done, and will later evolve into my games addon system. I wanted to test my script, so I built it. But I found a bunch of errors.
Includes:
You may notice I have a ton of includes at the top and you may think "Most of those arn't needed..." Well, I added the ones from console.cpp because I wanted to declare some console functions (which some were defined in console.cpp) so I thought, I just use all of the console.cpp ones for now, and figure out which one I need later. Well, if you can tell me first of all, which ones I really need, and second of all, how do I figure out which ones I need, it would be a great help.
So, finally, this is my script:
THE CPP FILE:
THE H FILE:
And, finally the errors:
I'm sorry if you think "This is a really stupid question..." but I just started to really get into C++, I've been reading an online book for about 4 hours, and I think that learning from my mistakes will help me greatly in the near future.
So I wrote a small script for a test. This script isn't done, and will later evolve into my games addon system. I wanted to test my script, so I built it. But I found a bunch of errors.
Includes:
You may notice I have a ton of includes at the top and you may think "Most of those arn't needed..." Well, I added the ones from console.cpp because I wanted to declare some console functions (which some were defined in console.cpp) so I thought, I just use all of the console.cpp ones for now, and figure out which one I need later. Well, if you can tell me first of all, which ones I really need, and second of all, how do I figure out which ones I need, it would be a great help.
So, finally, this is my script:
THE CPP FILE:
#include "addonSystem.h"
#include <iostream>
#include <string>
#include "platform/platform.h"
#include "platform/platformTLS.h"
#include "platform/threads/thread.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/consoleObject.h"
#include "core/stream/fileStream.h"
#include "core/resManager.h"
#include "console/ast.h"
#include "core/tAlgorithm.h"
#include "console/consoleTypes.h"
#include "console/telnetDebugger.h"
#include "console/simBase.h"
#include "console/compiler.h"
#include "console/stringStack.h"
#include "component/dynamicConsoleMethodComponent.h"
#include <stdarg.h>
#include "platform/threads/mutex.h"
using namespace std;
ConsoleFunctionGroupBegin( AddonSys_cons, "Functions to control and retrieve data from the addon system.");
ConsoleFunction( startupAddonSys, void, 1, 1, "Starts the addon system. DO NOT USE IN ADDONS! This is probably used at the launch of the game!")
{
if(!AddOnSys::m_StartedSys)
{
AddOnSys::init();
AddOnSys::m_StartedSys = true;
}
else
{
errorf("Add-on system already active.");
}
};
ConsoleFunction( executeClientAddons, void, 1, 1, "Executes client add-ons. Try not to do this more than once.")
{
AddOnSys::executeClientAddons();
};
ConsoleFunction( executeServerAddons, void, 1, 1, "Executes client add-ons. Try not to do this more than once.")
{
AddOnSys::executeServerAddons();
};
ConsoleFunction( executeMusic, void, 1, 1, "Executes all the music datablocks.")
{
AddOnSys::executeMusic();
};
ConsoleFuncton( retrieveMaps, static 1, 1, "Gets the map list.")
{
AddOnSys::retrieveMaps();
};
ConsoleFuncton( getAddonInfo, void 1, 1, "Echos the datablock data.")
{
AddOnSys::getAddonInfo();
};
//ConsoleFunction( executeServerAddons, bool, 2, 2, "(string text)"
// "Set the system clipboard.")
//{
//return Platform::setClipboard(argv[1]);
//};
ConsoleFunctionGroupEnd( AddonSys_cons);
static bool AddOnSys::init()
{
printf("- * - STARTING ADD-ON SYSTEM - * -");
return true;
}
static int AddOnSys::executeClientAddons()
{
printf("- * - Executing Client Add-Ons - * -");
return 1;
}
static int AddOnSys::executeServerAddons()
{
printf("- * - Executing Server Add-Ons - * -");
int currentAdded = 0;
m_addOnDatablocks += currentAdded;
return currentAdded;
}
static int AddOnSys::executeMusic()
{
printf("- * - Adding Music Datablocks - * -");
int addedMusic = 0;
m_MusicDatablocks += addedMusic;
return currentAdded;
}
static string AddOnSys::retrieveMaps()
{
printf("- * - Retrieving Maps - * -");
int mapsAdded = 0;
m_mapsAdded += mapsAdded;
return "hi";
}
void AddOnSys::getAddonInfo()
{
printf("- * - Add-On Info - * -");
printf("Maps: " + m_mapsAdded);
printf("Music: " + m_MusicDatablocks);
printf("Datablocks: " + m_addOnDatablocks);
printf("-----------------------");
}
static string AddOnSys::addonList()
{
return "NONE";
}
static string AddOnSys::musicList()
{
return "NONE MUSIC";
}
static int AddOnSys::getAddonDBdata()
{
int final = m_mapsAdded += m_MusicDatablocks += m_addOnDatablocks;
return final;
}THE H FILE:
//-----------------------------------------------------------------------------
// Torque Game Engine Advanced
// Copyright (C) GarageGames.com, Inc.
// Author: Timothy Volpe
//-----------------------------------------------------------------------------
#ifndef _APP_ADDONSYS_
#define _APP_ADDONSYS_
#include <string>
using namespace std;
class AddOnSys
{
public:
//starts up the addon system
static bool init();
//execute the addons for clients
static int executeClientAddons();
//execute the addons for the server
static int executeServerAddons();
//check for valid music and add it
static int executeMusic();
//gets the map list and returns it
static string retrieveMaps();
//gets the addon info, e.i. amount of datablocks, music and maps
void getAddonInfo();
//gets the addon list, each addon is separated by a line
static string addonList();
//gets the music list, each music is seoarated by a line
static string musicList();
//total amount of datablocks added
int getAddonDBdata();
static bool m_StartedSys;
static int m_MusicDatablocks;
static int m_addOnDatablocks;
static int m_mapsAdded;
static string s_addOnInfo;
static string s_addOnList;
static string s_musicList;
static string s_mapList;
//private:
};
#endifAnd, finally the errors:
1>.addonSystem.cpp(39) : error C3861: 'errorf': identifier not found
1>.addonSystem.cpp(58) : error C2065: 'retrieveMaps' : undeclared identifier
1>.addonSystem.cpp(58) : error C2059: syntax error : 'static '
1>.addonSystem.cpp(59) : error C2143: syntax error : missing ';' before '{'
1>.addonSystem.cpp(59) : error C2447: '{' : missing function header (old-style formal list?)
1>.addonSystem.cpp(63) : error C2065: 'getAddonInfo' : undeclared identifier
1>.addonSystem.cpp(63) : error C2059: syntax error : 'static '
1>.addonSystem.cpp(64) : error C2143: syntax error : missing ';' before '{'
1>.addonSystem.cpp(64) : error C2447: '{' : missing function header (old-style formal list?)
1>.addonSystem.cpp(77) : error C2724: 'AddOnSys::init' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(84) : error C2724: 'AddOnSys::executeClientAddons' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(90) : error C2724: 'AddOnSys::executeServerAddons' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(98) : error C2724: 'AddOnSys::executeMusic' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(102) : error C2065: 'currentAdded' : undeclared identifier
1>.addonSystem.cpp(106) : error C2724: 'AddOnSys::retrieveMaps' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(123) : error C2724: 'AddOnSys::addonList' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(128) : error C2724: 'AddOnSys::musicList' : 'static' should not be used on member functions defined at file scope
1>.addonSystem.cpp(133) : error C2724: 'AddOnSys::getAddonDBdata' : 'static' should not be used on member functions defined at file scopeI'm sorry if you think "This is a really stupid question..." but I just started to really get into C++, I've been reading an online book for about 4 hours, and I think that learning from my mistakes will help me greatly in the near future.
#2
If you were using Torque 3D, you would use #include "console/engineAPI.h". Torque 3D now makes use of a better system for exposing classes, variables, functions, and callbacks to script...far better.
08/08/2010 (4:46 pm)
As for your includes, you do not need them to use the ConsoleFunction macro. If you want to use paranoid inclusion, then just go with #include "console/console.h". If you were using Torque 3D, you would use #include "console/engineAPI.h". Torque 3D now makes use of a better system for exposing classes, variables, functions, and callbacks to script...far better.
#3
For the second parameter in this macro, you are using static 1 (???). That's a problem. You should be using the same return type for the original function.
Next, in executeMusic(), you are using a variable that has not been declared:
currentAdded does not exist. Declare it first, like you did in executeServerAddons(). Looks like you actually should have been using addedMusic, which did declare but never returned.
08/08/2010 (4:59 pm)
I found the source of some other errors. Check out the following:ConsoleFuncton( retrieveMaps, static 1, 1, "Gets the map list.")
For the second parameter in this macro, you are using static 1 (???). That's a problem. You should be using the same return type for the original function.
Next, in executeMusic(), you are using a variable that has not been declared:
return currentAdded;
currentAdded does not exist. Declare it first, like you did in executeServerAddons(). Looks like you actually should have been using addedMusic, which did declare but never returned.
#4
#include <string>
using namespace std;
any declarations of string ___
08/08/2010 (5:02 pm)
Here's a tip for general practice...do not use <string> and the string type. Use common char*, const char*, or the types provided by Torque. So, in your source files remove the following:#include <string>
using namespace std;
any declarations of string ___
#5
08/08/2010 (5:08 pm)
And finally, how you are using and exposing AddOnSys is a bit...off. You either need to be using a global extern, a singleton, or a namespace. You can look these up in your book, online, or look at how the other classes are handling this. If you only have a single instance of AddOnSys, you should consider making it a global or singleton.
#6
08/09/2010 (10:16 pm)
Ok, thanks a ton guys. I learned a lot ready these comments, ha. Hopefully in the near future if I run into these problems again, I'll know how to fix them. Once again, the amazing GarageGames community has saved me.
#7
08/09/2010 (10:46 pm)
Happy to help =)
#8
I can tell you that you likely won't use Mutex. Most times, I believe, that's used to stop multiple iterations of the same program from running.
Be careful including things that you aren't familiar with. As I've learned from experience, most of the time you'll just end up starting a wild goose chase long, long down the road.
08/09/2010 (11:41 pm)
I got here too late to see most of this...but I still want to be thanked!I can tell you that you likely won't use Mutex. Most times, I believe, that's used to stop multiple iterations of the same program from running.
Be careful including things that you aren't familiar with. As I've learned from experience, most of the time you'll just end up starting a wild goose chase long, long down the road.
Employee Michael Perry
ZombieShortbus
Con::errorf("Add-on system already active.");Same goes for printf and anything else used by the Con:: namespace. Making that change will resolve the 'errorf': identifier not found issues.
The next problem I see is that you are using the keyword static wrong. You only use it when the function is first declared:
Correct:
You did that right. However, when you define the function you should not use it a second time:
Wrong:
static bool AddOnSys::init() { printf("- * - STARTING ADD-ON SYSTEM - * -"); return true; }Right:
bool AddOnSys::init() { printf("- * - STARTING ADD-ON SYSTEM - * -"); return true; }Making those changes will resolve the 'static' should not be used on member functions defined at file scope problem.