"Variable" was not declared in this scope - c++

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.

Related

virtual method of header give errors during g++ compilation

I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !
You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.

How to avoid access errors while using friend functions in c++?

#include <iostream>
using namespace std;
class boss{
int salary;
public:
boss();
boss(int b){
salary = b;
}
friend void total(boss, employe);
};
class employe{
int salary;
friend void total(boss, employe);
public:
employe();
employe(int e){
salary = e;
}
friend void total(boss, employe);
};
void total(boss b, employe e){
int T;
T = b.salary + e.salary;
cout << T;
}
int main(){
boss bb(200);
employe ee(300);
total(bb,ee);
}
Errors I'm facing
16:31:40 **** Incremental Build of configuration Debug for project Practice ****
make all
'Building file: ../src/Practice.cpp'
'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Practice.d" -MT"src/Practice.o" -o "src/Practice.o" "../src/Practice.cpp"
../src/Practice.cpp:20:26: error: 'employe' has not been declared
20 | friend void total(boss, employe);
| ^~~~~~~
../src/Practice.cpp: In function 'void total(boss, employe)':
../src/Practice.cpp:38:8: error: 'int boss::salary' is private within this context
38 | T = b.salary + e.salary;
| ^~~~~~
../src/Practice.cpp:13:6: note: declared private here
13 | int salary = 3000;
| ^~~~~~
make: *** [src/subdir.mk:20: src/Practice.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
16:31:40 Build Failed. 3 errors, 0 warnings. (took 655ms)
The friend total(boss, employe) declaration in boss fails because employe hasn't been declared yet.
Add a forward declaration of employe above boss.
class employe; // <--- add this
class boss{
int salary = 3000;
public:
boss();
boss(int b){
salary = b;
}
friend void total(boss, employe);
};

C++ Template Class with Template Constructor

I tried to implement Properties in c++. I don't no why but if I want to compile my code there are quite a lot of errors. The main Idea was, that a template class and the tamplate constructor will give the requirement Informations.
I would be grateful if somebody could help me!
Compiling Message:
pi#raspberrypi ~/dev/property $ gcc -std=c++0x -o PropertyTest2 PropertyTest2.cpp
PropertyTest2.cpp:22:16: error: expected ‘;’ at end of member declaration
PropertyTest2.cpp:22:19: error: expected unqualified-id before ‘<’ token
PropertyTest2.cpp: In function ‘int main()’:
PropertyTest2.cpp:34:20: error: use of deleted function ‘PropertyTestClass::PropertyTestClass()’
PropertyTest2.cpp:8:7: error: ‘PropertyTestClass::PropertyTestClass()’ is implicitly deleted because the default definition would be ill-formed:
PropertyTest2.cpp:8:7: error: no matching function for call to ‘Property<int>::Property()’
PropertyTest2.cpp:8:7: note: candidates are:
Property4.cpp:21:2: note: template<int (** G)(), void (** S)(int&)> Property::Property()
Property4.cpp:6:7: note: constexpr Property<int>::Property(const Property<int>&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
Property4.cpp:6:7: note: constexpr Property<int>::Property(Property<int>&&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
PropertyTest2.cpp:38:20: error: no matching function for call to ‘Property<int>::Set(int)’
PropertyTest2.cpp:38:20: note: candidate is:
Property4.cpp:30:7: note: void Property<T>::Set(T&) [with T = int]
Property4.cpp:30:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’
Property Class (Property.cpp)
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
template <class T>
class Property {
private:
typedef T (*TGetter)(void);
typedef void (*TSetter)(T &);
TGetter Getter;
TSetter Setter;
public:
typedef T type;
template<TGetter *G,
TSetter *S
>
Property() {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(T &value) {
(this->Setter)(value);
}
};
#endif
Testing file (PropertyTest.cpp):
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter(void) {
return this->_age;
}
void AgeSetter(int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age<&PropertyTestClass::AgeGetter, &PropertyTestClass::AgeSetter>;
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Ok, this time fixed all the problems in your code.
Property.cpp:
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
#include <boost/function.hpp>
template <class T>
class Property {
private:
typedef boost::function <T()> TGetter;
typedef boost::function <void(const T&)> TSetter;
TGetter Getter;
TSetter Setter;
public:
typedef T type;
Property(TGetter G, TSetter S) {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(const T &value) {
(this->Setter)(value);
}
};
#endif
PropertyTests.cpp:
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include <boost/bind.hpp>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter() {
return this->_age;
}
void AgeSetter(const int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age;
PropertyTestClass() : age(
boost::bind(&PropertyTestClass::AgeGetter, this),
boost::bind(&PropertyTestClass::AgeSetter, this, _1))
{}
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Output:
$ ./a.out
Property Test Programm
ReadWrite Property for age
5

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.

trouble redefining print function in lua

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.