trouble redefining print function in lua - c++

I am attempting to redefine the print function as described in this question. Here is my code:
extern "C"{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <iostream>
using namespace std;
lua_State* L;
static int l_my_print(lua_State* L) {
int nargs = lua_gettop(L);
for (int i=1; i <= nargs; i++) {
if (lua_isstring(L, i)) {
cout << "!!!" << lua_tostring(L, i) << "!!!" << endl;
}
}
return 0;
}
static const struct luaL_Reg printlib [] = {
{"print", l_my_print},
{NULL, NULL} /* end of array */
};
extern int luaopen_luamylib(lua_State *L)
{
lua_getglobal(L, "_G");
luaL_register(L, NULL, printlib);
lua_pop(L, 1);
}
int main(){
L = luaL_newstate();
luaL_openlibs(L);
luaopen_luamylib(L);
luaL_dostring(L, "print(\"hello\")");
lua_close(L);
return 0;
}
When I attempt to compile the code, I get:
$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2
test.cpp:28:1: error: elements of array ‘const luaL_reg printlib []’ have incomplete type
test.cpp:28:1: error: storage size of ‘printlib’ isn’t known
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’:
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type]
Can anyone explain what is occurring here? am I missing a library or something?
UPDATE
It was pointed out that the struct is called luaL_Reg, not luaL_reg. This solved my first problem:
$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’:
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type]

First error: It's luaL_Reg, not luaL_reg.
Second error:
luaL_register is deprecated (in Lua 5.2), and is only available if LUA_COMPAT_MODULE is defined before you include the Lua headers. You should use luaL_setfuncs instead.

Related

Could not compile c++ program with threads support on AIX with GCC compiler 4.7.3

I have problem when compiling code below on aix machine with gcc compiler (version 4.7.3):
SomeThread.h
#ifndef SomeThread_H
#define SomeThread_H
class SomeThread {
public:
SomeThread(void);
virtual ~SomeThread(void);
void runThread();
}; // SomeThread
#endif // _SomeThread_H_
SomeThread.cpp
#include "SomeThread.h"
#include <thread>
#include <iostream>
using namespace std;
namespace {
void foo_thread_function() {
for (int i = 0; i < 10; ++i) {
cout << "Some threaded text" << endl;
}
}
}
SomeThread::SomeThread() {
} // SomeThread
SomeThread::~SomeThread() {
} // ~SomeThread
void SomeThread::runThread() {
thread foo_thread_01(foo_thread_function);
thread foo_thread_02(foo_thread_function);
thread foo_thread_03(foo_thread_function);
foo_thread_01.join();
foo_thread_02.join();
foo_thread_03.join();
}
The error which I get is following:
SomeThread.cpp: In member function 'void SomeThread::runThread()':
SomeThread.cpp:58:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
from /usr/include/sys/proc.h:42,
from /usr/include/sys/pri.h:43,
from /usr/include/sys/sched.h:38,
from /usr/include/sched.h:51,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
from SomeThread.cpp:5:
/usr/include/sys/thread.h:105:8: error: candidates are: struct thread
In file included from SomeThread.cpp:5:0:
/opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:60:9: error: class std::thread
SomeThread.cpp:58:11: error: expected ';' before 'foo_thread_01'
SomeThread.cpp:59:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
from /usr/include/sys/proc.h:42,
from /usr/include/sys/pri.h:43,
from /usr/include/sys/sched.h:38,
from /usr/include/sched.h:51,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
from SomeThread.cpp:5:
I compile the above files with following command line:
g++ -maix64 -DTARGET=target_thread -DGENDATE=04_01_2017 -DTT_LIB_DLLSUFFIX=\".so\" -DOSNAME=\"AIX\" -D_GNU_SOURCE -D_REENTRANT -DAIX -Wno-deprecated -I. -std=gnu++11 -maix64 -pthread -mminimal-toc -fpermissive -Wno-write-strings -Winvalid-offsetof -O3 -c -oSomeThread.o SomeThread.cpp
The problem is, that there are multiple implementations of thread with your compiler options and includes. Maybe it would be just enough to correct the code to this.
SomeThread.cpp
#include "SomeThread.h"
#include <thread>
#include <iostream>
//Stop using namespace std, please
namespace SomeNamespace {
void foo_thread_function() {
for (int i = 0; i < 10; ++i) {
cout << "Some threaded text" << endl;
}
}
}
SomeThread::SomeThread() {
} // SomeThread
SomeThread::~SomeThread() {
} // ~SomeThread
void SomeThread::runThread() {
std::thread foo_thread_01(SomeNamespace::foo_thread_function);
std::thread foo_thread_02(SomeNamespace::foo_thread_function);
std::thread foo_thread_03(SomeNamespace::foo_thread_function);
foo_thread_01.join();
foo_thread_02.join();
foo_thread_03.join();
}
Ambiguous means that there are multiple interpretations of the same word.
Example:
namespace Bla{
struct SomeStruct{
}
}
namespace Blub{
struct SomeStruct{
}
}
int main(){
using namespace Bla;
using namespace Blub;
SomeStruct ImAmbiguous; // Problem now, which struct should the compiler choose now?
Bla::SomeStruct structFromBla; //Now the compiler knows which struct should be choosen
return 0;
}

"Variable" was not declared in this scope

I am getting this error when I try to compile my class. I have made sure there is function prototypes and variables are initilized correctly, hopefully someone can help me figure out the problem
g++ -c main.cc
g++ -c BankControl.cc
g++ -c Bank.cc
g++ -c Account.cc
g++ -c View.cc
g++ -c AcctList.cc
g++ -c Customer.cc
g++ -c CustArray.cc
g++ -c Transaction.cc
Transaction.cc: In function ‘int getTransID()’:
Transaction.cc:18:34: error: ‘transID’ was not declared in this scope
int getTransID() { return transID; }
^
Transaction.cc: In function ‘TransType getTType()’:
Transaction.cc:19:34: error: ‘tType’ was not declared in this scope
TransType getTType() { return tType; }
^
Transaction.cc: In function ‘TransState getTState()’:
Transaction.cc:20:34: error: ‘tState’ was not declared in this scope
TransState getTState() { return tState; }
^
Transaction.cc: In function ‘std::__cxx11::string getDate()’:
Transaction.cc:21:34: error: ‘date’ was not declared in this scope
string getDate() { return date; }
^
Transaction.cc: In function ‘int getTAcctNum()’:
Transaction.cc:22:34: error: ‘tAcctNum’ was not declared in this scope
int getTAcctNum() { return tAcctNum; }
^
Transaction.cc: In function ‘float getTAmount()’:
Transaction.cc:23:34: error: ‘tAmount’ was not declared in this scope
float getTAmount() { return tAmount; }
^
Transaction.cc: In function ‘void setDate(std::__cxx11::string)’:
Transaction.cc:28:3: error: ‘date’ was not declared in this scope
date = d;
^
Makefile:31: recipe for target 'Transaction.o' failed
make: *** [Transaction.o] Error 1
Here is my header file
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
using namespace std;
#include "defs.h"
class Transaction
{
public:
Transaction(TransType = TTERROR, TransState = TSERROR,int = 0 ,float = 0);
int getTransID();
TransType getTType();
TransState getTState();
string getDate();
int getTAcctNum();
void setDate(string);
float getAmount();
private:
static int nextTransID;
int transID;
TransType tType;
TransState tState;
string date;
int tAcctNum;
float tAmount;
};
#endif
Here is my source file
#include "Transaction.h"
#include "defs.h"
#include <string>
using namespace std;
int Transaction::nextTransID = 2001;
Transaction::Transaction(TransType t, TransState s, int acct, float amount)
{
transID = nextTransID++;
tType = t;
tState = s;
tAcctNum = acct;
tAmount = amount;
}
int getTransID() { return transID; }
TransType getTType() { return tType; }
TransState getTState() { return tState; }
string getDate() { return date; }
int getTAcctNum() { return tAcctNum; }
float getTAmount() { return tAmount; }
void setDate(string d)
{
date = d;
}
I am kinda lost on what is the issue
This:
int getTransID() { return transID; }
has nothing to do with your class, it's a global function.
You meant:
int Transaction::getTransID() { return transID; }
Also, that function (and the other getters) should be made const to signal that they don't modify the object.

Error with a function pointer as a map parameter? C++

I seem to be getting several errors with
map<string,function<XMLSerializable*()>> mapConstructor;
Notably,
la5.cpp: In function ‘int main(int, char**)’:
la5.cpp:21:13: error: ‘function’ was not declared in this scope
la5.cpp:21:43: error: ‘mapConstructor’ was not declared in this scope
la5.cpp:21:43: error: template argument 2 is invalid
la5.cpp:21:43: error: template argument 4 is invalid
la5.cpp:25:58: warning: lambda expressions only available with -std=c++0x or - std=gnu++0x [enabled by default]
la5.cpp:33:26: error: expected primary-expression before ‘*’ token
la5.cpp:33:28: error: expected primary-expression before ‘)’ token
la5.cpp:33:31: error: ‘pFunc’ was not declared in this scope
make: *** [la5.o] Error 1
Unfortunately, I can't seem to find what I've done wrong, as it seems to deal with that map declaration which was given to the class by my instructor. Below is my .cpp
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include "Armor.h"
#include "Weapon.h"
#include "Item.h"
#include "Creature.h"
using namespace std;
XMLSerializable * constructItem()
{
return new Item;
}
int main(int argc, char * argv[])
{
map<string,function<XMLSerializable*()>> mapConstructor;
mapConstructor["Item"] = constructItem;
mapConstructor["Creature"] = []() {return new Creature; };
cout << "Input the class name, then we'll try to construct it." << endl;
string sLookup = " ";
cin >> sLookup;
function<XMLSerializable*()> pFunc = mapConstructor[sLookup];
if(pFunc() == NULL)
{
cout << "Sorry, the object couldn't be constructed." << endl;
}
else
{
cout << pFunc() << " a non NULL value was returned!" << endl;
}
return 0;
}
Any suggestions? I'm unfamiliar with maps, but I believe this should work, right?
Coding in pico, compiling with a makefile using g++.
It looks like you just are forgetting to add -std=c++11 or -std=c++0x to your compiler flags to enable C++11.
-std=c++0x is deprecated, but on older versions of g++, -std=c++11 is not available.

auto keyword is not deducing type of string

I've this simple code:
#include <string>
#include <iostream>
using namespace std;
int main() {
string s1 = "rohit";
auto len = s1.size();
cout << len;
}
When I compile this code on Ubuntu 12.04, it shows following error:
test.cc: In function ‘int main()’:
test.cc:8:10: error: ‘len’ does not name a type
auto len = s1.size();
^
test.cc:10:13: error: ‘len’ was not declared in this scope
cout << len;
^
I've got g++ 4.8.1. Is there some changes with the usage of auto keyword in g++ 4.8.1?
The error: ‘len’ does not name a type leads me to believe that you didn't compile in C++11 mode and that it's using the old, C++98 meaning of the keyword auto.

Getting CppUnit to read application class on netbeans 7.2

I am learning C++ and CppUnit at the same time, using netbeans 7.2.
I create the following file
#include <cstdlib>
using namespace std;
/*
*
*/
class Subtract{
public:
int minus(int a, int b){
return a-b;
}
};
int main(int argc, char** argv) {
return 0;
}
And then I right-click to generate the following cppunit test file
#include "newtestclass.h"
CPPUNIT_TEST_SUITE_REGISTRATION(newtestclass);
newtestclass::newtestclass() {
}
newtestclass::~newtestclass() {
}
void newtestclass::setUp() {
}
void newtestclass::tearDown() {
}
int Subtract::minus(int a, int b);
void newtestclass::testMinus() {
int a=89;
int b=55;
Subtract subtract;
int result = subtract.minus(a, b);
CPPUNIT_ASSERT_EQUAL(34,result);
}
When I try to run the test, it gives the following errors
g++ -c -g -I. -MMD -MP -MF build/Debug/GNU-MacOSX/tests/tests/newtestclass.o.d -o build/Debug/GNU-MacOSX/tests/tests/newtestclass.o tests/newtestclass.cpp
tests/newtestclass.cpp:25: error: 'Subtract' has not been declared
tests/newtestclass.cpp: In member function 'void newtestclass::testMinus()':
tests/newtestclass.cpp:30: error: 'Subtract' was not declared in this scope
tests/newtestclass.cpp:30: error: expected `;' before 'subtract'
tests/newtestclass.cpp:31: error: 'subtract' was not declared in this scope
make[1]: *** [build/Debug/GNU-MacOSX/tests/tests/newtestclass.o] Error 1
make: *** [.build-tests-impl] Error 2
How do I get this to work properly?
In C++, the convention is to declare the classes and functions in a header file (.h file) and implement them in the source file (.cpp file).
Your Subtract.h file (declarations) should have only this:
class Subtract {
public:
int minus(int a, int b);
};
Your Subtract.cpp file (implementation) should have this:
#include "Subtract.h"
int Subtract::minus(int a, int b)
{
return a-b;
}
Then you #include "Subtract.h" in your newtestclass.cpp file.