Thanks in advance to everyone who is trying to help.
I want to declare an empty object in the main as follows:
The object has primitive fields and composite fields, all are private.
what is the correct signature of the constructor in the *.h file?
for example if I have class Rectangle, which contains a height and width fields among 4 Points (the composition) it would be something like:
#include "point.h" //Standard representation of a point
class Rectangle
{
private:
Point bRight, bLeft, uRight, uLeft;
double height, width;
public:
Rectangle(double, double, Point, Point, Point, Point)
... other not relevant functions
}
and on the main function:
#include "Point.h"
#include "Rectangle.h"
int main()
{
Rectangle r1(); //I want this row to invoke the constructor mantioned.
//above without implementing an empty one.
}
For the primitive fields it is quiet understood, you just put default values in the signature at the *.h file.
something like:
Rectangle(double = 0, double = 0, Point, Point, Point, Point)
How can I put default values into the Points?
Not so related question: I've noticed that when I wrote in the main
"Rectangle r1();" it compiled although I didn't assigned default values to any
of the fields nor implemented the empty constructor, and when I debugged it the compiler wouldn't let me step into that line, why is that?
How can I put default values into the Points?
You can do it as mentioned in the comment:
Rectangle ( double = 0, double = 0, Point = Point()
, Point = Point(), Point = Point(), Point = Point())
Not so related question: I've noticed that when I wrote in the main
Rectangle r1(); it compiled although I didn't assigned default values to any
of the fields nor implemented the empty constructor, and when I debugged it the compiler wouldn't let me step into that line, why is that?
It's not constructing an instance of Rectangle, but declares a function. To construct a Rectangle with default constructor, omit the parenthesis:
Rectangle r;
1) A default argument can be pretty much anything, it does not need to be a literal. Including Point(), as mentioned in the comments.
2) Rectangle r1(); declares r1 as a function taking no parameters and returning Rectangle. There is no initialisation there, there is no executable code there, so there is nothing to step into. There is no way to change the language so that Rectangle r1(); declares r1 as an object. To declare it as an object, but force it to be initialised regardless of its type, in the current standard, you can use {} rather than (). Older compilers may not support this and treat it as a syntax error.
I am trying to unit test a C++ application that I am building and I'm having an issue initializing the array when used in a class. I've tried alot of different methods of loading this information, the only ones that work are inefficient / not suitable.
Here is the hex array that I have (randomised the parts)
0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10
And header file for my unit test:
class MessageParsingTest : public CPPUNIT_NS::TestFixture {
CPPUNIT_TEST_SUITE(MessageParsingTest);
CPPUNIT_TEST(testIdentifyFirstMessageType);
CPPUNIT_TEST_SUITE_END();
public:
MessageParsingTest();
virtual ~MessageParsingTest();
void setUp();
void tearDown();
private:
void testIdentifyFirstMessageType();
void testIdentifySecondMessageType();
// data members for the hex array
unsigned char firstMessage[1500];
};
Then in my test case setUp function;
void MessageParsingTest::setUp() {
firstMessage = {0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10};
}
That it my latest failed attempt, it says its not valid during compilcation, as I expected, but at this point I was trying anything.
I've also tried things like (all in setUp function)
firstMessage << "\0x24\0x54\0x3b\0x72\0x8b\0x03\0x24\0x29\0x23\0x43\0x66\0x22\0x53\0x41\0x11\0x62\0x10";
firstMessage[1500] = "\0x24\0x54\0x3b\0x72\0x8b\0x03\0x24\0x29\0x23\0x43\0x66\0x22\0x53\0x41\0x11\0x62\0x10";
and a few other crazy ways, Does anyone know the proper way to load this data? the only way I've had it working so far is with either no data member declaration and straight up defining it and initializing in one line (but then I cant access in the test cases) or doing it one by one like firstMessage[0] = 0x24; etc.
I understand that there will be a simple, proper way of doing this and considering what the application actually does, this part should be the easiest.
You have few options:
Initialize arrays in constructor MesssageParsingTest using syntax : firstMessage{0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10}
in initializer list.
Create static const array containing your message, and either copy it to member variable using memcpy, or use static member and get rid of firstMessage member variable.
Declare const static member in .h inside class definition:
static const unsigned char kFirstMessage[];
and define + initialize it in .ccp
const unsigned char MessageParsingTest::kFirstMessage[] = "\0x24\0x54\0x3b\0x72\0x8b\0x03\0x24\0x29\0x23\0x43\0x66\0x22\0x53\0x41\0x11\0x62\0x10";
I would prefer static const member if you do not intend to modify this array later, since it makes the intention cleaner.
Here is one way to do it.
void MessageParsingTest::setUp()
{
unsigned char x[] = {0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10};
::memcpy(firstMessage, x, sizeof(x));
}
If you are using C++11, you can also initialize the firstMessage in the class member initialization list as
MessageParsingTest::MessageParsingTest() :
firstMessage{0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10},
...
You can use a temporary buffer and then copy into you member as this:
void MessageParsingTest::setUp() {
unsigned char tmp[1500] = {0x24,0x54,0x3b,0x72,0x8b,0x03,0x24,0x29,0x23,0x43,0x66,0x22,0x53,0x41,0x11,0x62,0x10};
memcpy(firstMessage, tmp, 1500);
}
So, I am working on a side-project to keep my c++ skills fresh (it has been many years since I have done work in c++). I am working on something where I will be using SQLite. I have a wrapper around the SQLite code. One of the things I am noticing is that SQLite uses c-style callback functions in its sqlite3_exec(...) function.
I would like to have the callback function be an object method, as I would like it to be able to modify object variables but am unsure of how to do this exactly. I have checked other similar questions on stackoverflow but came away with nothing helpful.
Here is how I am declaring my wrapper class:
class DBAdapter
{
private:
sqlite3* db;
int getUserRecords(std::string);
std::vector<USER_RECORD> records;
int callbackSel(void*, int , char**, char**);
public:
DBAdapter();
~DBAdapter();
int open(std::string);
void close();
int insertRecord();
int deleteRecord();
int getNumUserRecords();
};
Here is how I am trying to use the callback (callbackSel), from within getNumUserRecords:
int DBAdapter::getUserRecords(std::string name)
{
std::string sql = "SELECT" + name + " from USERS";
char* ErrMsg;
char* data;
int retval = sqlite3_exec(db,sql.c_str(),this->callbackSel,data,&ErrMsg);
return retval;
}
The error message I am getting is:
error: ‘int (* DBAdapter::callbackSel)(void*, int, char**, char**)’ is not a static member of ‘class DBAdapter’
My problem is, if I make this a static function, I won't be able to have access to my vector, records, right? Is there any way around this?
Is there any way around this?
I don't know for sqlite API specifically, but usually c-style callbacks support to have user data passed from a void* pointer (I'd guess the 1st parameter of that signature you mention). What one typically does is:
Declare a static class function to specify as callback function pointer
Implement that function to cast the passed user data pointer to a pointer to your class instance and call a member function
Have a member function defined in the class that provides the implementation you want
Pass a pointer to your handling class instance, when you're going to register the static member function pointer with the C API
I hope this points out the right direction.
My main program is to generate a random number to create movement of a object in a 2 dimensional array and to keep track of it.
one of my function void current_row(int row){position = row}; keeps track of the current row of the object.
since the variable is not global. i am finding problems calling the current location and updating it to the next movement. this is how the other function may look like:
void movement (){
int row;
row = current_row();
/*
* Here is the problem i'm having. This may well be
* a third function which has the same information
* as my first function. But still how do I access
* once without modifying it and access it
* again to update it?
*/
// call another function that creates new row.
// update that info to the row
}
i am new to c++.
Use an instance variable to keep track of it. That's why instance variables exist: To hold their values between function calls.
In case it's an OOP environment (as C++ tag implies), some class should declare int row as a class member (including a getter and a setter as methods).
Another option is declaring the variable at the head of the main() part of the program and call functions with row as a function parameter.
void movement(int row)
{
}
You can consider the parameter be passed by reference if you are intending to change it, otherwise it would be better declaring it const inside the function parameter declaration. If part of the answer sounds unfamiliar to you I would suggest reading through :
What's the difference between passing by reference vs. passing by value?
I have 3 files pos.h pos.cpp and main.cpp .... I am trying to call a function from pos.cpp in the main class for instance :
pos.h file
class pos {
public:
pos(); //defualut constructor
int open_port();
}
pos.cpp
#include "pos.h"
int Open_port() {
//do stuff here
return 0;
}
class main.cpp
#include "pos.h"
int main(int argc , char** argv) {
pos pos1;
pos1::Open_port();
}
Problem is I always get that pos1 is not a class or namespace I am compining as follows
g++ mainpos.cpp pos.cpp pos.h -o position -lpthread
Any thoughts ?
You seems to have several issues in the code:
int open_port();
is a member function of pos. However, when you define it, you are not using :: operator and the function name is changed.
Try:
int pos::open_port()
{ ///^^pay attention to typos
//do stuff here
return 0;
}
Then inside main. you can do:
pos pos1;
pos1.open_port();
If you really mean Open_port(), which is not a member of the class, then you need to add function declaration into the proper header files and use it properly, but that's a separate issue.
You've several problems, most of them related to fundamental syntax:
Case matters. open_port and Open_port are two completely different things
You're not actually defining the method of the class pos, you're making a new function
int Open_port() {
needs to be
int pos::open_port() {
You're trying to invoke a non-static method statically. You need to create an instance of pos, (which you've done, pos1) and invoke open_port on it via pos1.open_port(). You cannot call pos::open_port directly, unless you declare the method static.
A final problem would be that you've declared but not defined the default constructor for your class. You need to provide a method body for pos::pos().
Well, you have two problems with your code. In the cpp file you need to use the scope for that function, so you need to have:
int pos::Open_port()
Also, you need to ensure that open_port and Open_port are spelled and capitalized the same.
Last thing, if you want to call open_port like that, you'll need to decare the function as static in the class def'n.