Hi,
can anybody see why this code won't work? This is my PrintTest class:
// header file
#pragma once
#include <iostream>
#include <string>
class PrintTest
{
public:
PrintTest(void);
~PrintTest(void);
void ConstructPrintTest(PrintTest* pt);
void DestructPrintTest(PrintTest* pt);
void printText(void);
void printText(std::string sometext);
};
// .cpp file
#include ".\printtest.h"
PrintTest::PrintTest(void)
{
std::cout << "in printtest constructor" << std::endl;
}
PrintTest::~PrintTest(void)
{
std::cout << "in printtest destructor" << std::endl;
}
void PrintTest::ConstructPrintTest(PrintTest* pt)
{
new(pt) PrintTest();
}
void PrintTest::DestructPrintTest(PrintTest* pt)
{
pt->~PrintTest();
}
void PrintTest::printText(void)
{
std::cout << "call of method without arguments" << std::endl;
}
void PrintTest::printText(std::string sometext)
{
std::cout << sometext << std::endl;
}
And this is how I initialze AngelScript and register the void PrintTest::printText(void) method:
#include "angelscript.h"
#include "PrintTest.h"
#include <assert.h>
#include <string>
#include <fstream>
std::string LoadScript(const std::string &scriptfile);
class COutputStream : public asIOutputStream
{
public:
void Write(const char *text) { std::cout << text; }
};
int main()
{
int r = 0;
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if(engine == 0)
std::cout << "could not create the script engine" << std::endl;
r = engine->RegisterObjectType("PrintTest", sizeof(PrintTest), asOBJ_CLASS_CD);
assert(r >= 0);
r = engine->RegisterObjectBehaviour("PrintTest", asBEHAVE_CONSTRUCT, "void f()",asMETHOD(PrintTest, ConstructPrintTest), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectBehaviour("PrintTest", asBEHAVE_DESTRUCT, "void f()",asMETHOD(PrintTest, DestructPrintTest), asCALL_THISCALL);
assert(r >= 0);
//r = engine->RegisterObjectMethod("PrintTest", "void printText()", asMETHODPR(PrintTest, printText,(void), void ), asCALL_THISCALL);
r = engine->RegisterObjectMethod("PrintTest", "void printText(void)", asMETHOD(PrintTest, printText), asCALL_THISCALL);
assert(r >= 0);
std::string script = LoadScript("testscript.txt");
r = engine->AddScriptSection("module", "test", script.c_str(), script.length());
assert(r >= 0);
COutputStream out;
r = engine->Build("module", &out);
assert(r >= 0);
int funcID = engine->GetFunctionIDByDecl("module", "void test()");
asIScriptContext* testContext = 0;
engine->CreateContext(&testContext);
return 0;
}
std::string LoadScript(const std::string &scriptfile)
{
std::ifstream fin(scriptfile.c_str());
char ch;
std::string script_contents;
while(fin.get(ch))
{
script_contents += ch;
}
fin.close();
return script_contents;
}
This is my script file:
void test()
{
PrintTest pt;
pt.printText(void);
}
The assertion at engine->Build() fails. The out stream says:
test (4, 15) : Error : Expected identifier
Whats wrong with my script? when I comment out pt.printText(void);
then suddenly everything works.
Keep up the great wik with AS!
thanks,
SomeFusion