|
I think I found the problem:
[^].
|
|
|
|
|
Apparently compiler does not like to have both constructor and class variable names <b>to be same</b>.
this->variable_name = variable_name
I am using "this" pointer to pass / copy parameters passed to class constructor so they can be accessed by class methods.
I have managed to do it for simple int
Here is the declaration of int disp_x_size_, disp_y_size_; in the class
int disp_x_size_, disp_y_size_; //size x y
int *ControlPin; // control pins array
int *DB; // data bus pins array
and here is how I use "this" pointer in the constructor :
this->disp_x_size_ = disp_x_size_; // class local variable = paramater passed to constructor
works fine.
Now I tried to do same for passed pointer - for ControlPin declaration see above and here is the "this" pointer code
this->ControlPin = ControlPin;
this->DB = DB;
Compiles OK , but when I try to use it all I am getting is the actual content of the first member of the array. I have limited ways to check the pointer.
When I try to actually use it in this function
LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position
it does not compile with the following error
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h: In member function 'void TFT_LCD::setXY(word, word, word, word)':
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h:9297:87: error: no matching function for call to 'TFT_LCD::LCD_Write_COM_DATA_t(<b>int*&, int*&,</b> uint32_t, uint32_t, int)'
LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position
I believe that instead of simple int* I am passing the contents of the first array member int*&
which obviously is not the TYPE expected by the template function.( And I can verify that)
BTW that function works when correct TYPE pointer is passed to it.
I basically need someone to explain to me what am I doing wrong using "this" pointer that I end up with int*& instead of just int*.
I hope it makes sense, if not I can explain it more. But don't ask for full code - it is now over 10000 lines of giant mess
Appreciate any help, as always
Cheers
Vaclav
-- modified 20-Sep-15 11:25am.
|
|
|
|
|
Vaclav_Sal wrote: what am I doing wrong using "this" Nothing. this is merely a pointer to the data space of 'this' object, when in the code of that object, i.e. inside the methods declared in the class. Using it in the constructor has no relevance to your problem. The problem is that your call to LCD_Write_COM_DATA_t contains parameters that do not match the declaration of the method.
Taker a look at Pointer to Pointer and Reference to Pointer[^] for an explanation.
|
|
|
|
|
"contains parameters that do not match the declaration of the method."
Sorry, but it is just the opposite.
The method is OK, it's the wrong parameters I am passing in and I really do not know why.
I do not want contents , I need pointer to the array.
If I change the method I would have to rebuild it so it will take a contents, not pointer.
I'll take a look at the reference, thanks you for that.
|
|
|
|
|
Vaclav_Sal wrote: The method is OK, it's the wrong parameters I am passing in That's what I said.
|
|
|
|
|
Vaclav_Sal wrote: Apparently compiler does not like to have both constructor and class variable names to be same Not true, it does not matter. That is why you need to use the this pointer, so the compiler knows which variable you are referring to. Consider the following:
Class Foo
{
int var1;
foo(int var1)
{
var1 = var1; this->var1 = var1; }
None of which has any relevance to the issue you are describing. Forget about what goes on in the constructor, it is not connected to the probelm. You need to go back to the documentation and check carefully the definition of the TFT_LCD::LCD_Write_COM_DATA_t method.
|
|
|
|
|
If i made a program which prints on the screen " Mike " infinite times without stop . how can i define a certain key if i pressed the program stop printing ?
======
I use ' getch() ' method but the program had to stop and check my entry and this isn't my question here.
modified 19-Sep-15 2:44am.
|
|
|
|
|
You have to include some code inside the loop to recognise the keystroke or some other event to break out of the loop. If you add a getch then that would pause the loop until the keystroke is delivered. To get round this you can put that code in a separate thread and set a flag for the loop to check. You could also use _kbhit[^], although it has limitations.
|
|
|
|
|
if i'm not mistaken ctrl c(yes the copy shortcut) should break an infinite loop
|
|
|
|
|
How can i define the enter key to stop a working loop in my program ?
for advance ..
2-programs here which enter key defined both with different ways !
==============
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int x=1;
r:
printf("Allah %d\n", x);
x++;
while ( getch() != '\r')
{
goto r;
}
return 0;
}
and the other one ..
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int nm = 0, ch = 0, wr = 1,i=0;
char x;
while ((x=getchar()) != '\n')
{
if (isdigit(x))
{
nm++;
}
else if (isalpha(x))
{
ch++;
}
else if (x == ' ')
{
wr++;
}
i++;
}
printf(" \n");
printf("Numbers entered = %d , Characters entered = %d , Words entered = %d", nm, ch, wr);
printf(" \n");
system("pause");
return 0;
}
The first one enter was defined as '\r' while the second was '\n' ! which is the right way ?!
|
|
|
|
|
What happens when you try it?
|
|
|
|
|
Both programs debug well ! :/
|
|
|
|
|
I have the following code and when running it stays blocked on the join(). So, is it possible to call std::thread::join() in the destructor of a global variable?
void MyTimerFunction();
class MyGlobal
{
public:
std::atomic<bool> m_Flag;
std::thread m_GlobalThread;
MyGlobal() {
m_Flag = true;
m_GlobalThread = std::thread(MyTimerFunction);
}
~MyGlobal()
{
m_Flag = false;
if (m_GlobalThread.joinable())
m_GlobalThread.join();
std::cout << "MyGlobal destroyed" << std::endl;
}
};
MyGlobal Test;
void MyTimerFunction()
{
do
{
std::this_thread::sleep_for(std::chrono::seconds{10});
if (!Test.m_Flag)
break;
std::cout << "New tick" << std::endl;
} while (true);
std::cout << "Exit MyTimerFunction" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Main thread sleeping..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds{15});
std::cout << "Main thread awaking..." << std::endl;
std::cout << "Main thread returning..." << std::endl;
return 0;
}
|
|
|
|
|
Quote: it stays blocked on the join()
That's the correct behaviour: a never-ending thread won't join.
|
|
|
|
|
Not exactly, the destructor of the global variable sets an atomic to tell the thread to exit.
|
|
|
|
|
|
Yeh, it seems to be the same thing. I checked under Linux with GCC and it works. On my point of view, Visual C++ runtime after exiting the main() destroys all the threads then all the global variables, instead of the opposite.
|
|
|
|
|
Looks a sensible hypothesis to me.
|
|
|
|
|
I need a sorted list of strings with some additional information for each entry, so I thought about to use std::map
I am doing just simple things:
Definition:
typedef std::map<std::string, WORD> tTABMNEMO;
tTABMNEMO TabMnemo;
and in constructor of using class I am calling firstly:
TabMnemo.clear();
already this first access (in constructor of using class) causes an access violation.
any idea, what could be wrong?
Do I need to initialize anyhow?
maybe it is because of my program structure? this map is defined globally outside of the class and all the member functions of the using class are static functions!
|
|
|
|
|
Member 8534035 wrote:
TabMnemo.clear(); Have you tried:
try
{
TabMnemo.clear();
}
catch(exception &e)
{
cout << e.what();
}
Member 8534035 wrote: ...and all the member functions of the using class are static functions! So have you tried using it in a class with non-static functions?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
In my opinion that shouldn't happen. Are you sure the access violation is triggered by the clear method invocation?
Could you opst the exact code?
|
|
|
|
|
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor.
I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore.
I can rebuild these methods to include constructor passed parameters. no issue there.
But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies.
I hope I have explained my dilemma , if not , sorry.
Thanks Vaclav
( C/C++ and gcc)
|
|
|
|
|
Could you please post sample code?
|
|
|
|
|
It is normal practice to add parameters that remain constant in the object to the constructor like:
class Foo
{
private:
int paramThatDoesNotChangeOften;
public:
Foo(int someParam)
{
paramThatDoesNotChangeOften = someParam);
}
};
You could then add a getter method to return the value to the user if it's value is needed outside the class.
Is that what you are querying?
|
|
|
|
|
Thanks for replies.
It may be stupid, but that is what I am trying to avoid.
Now I would have "someParam" - actually global array and a class copy of the same so it can be accessed by class methods.
I am trying to save some memory of embedded processor.
Kinda of wishful thinking ahead.
I won't get to upset if I need to make a local copies.
Would using "this" pointer work making local copy?
I just tryied "standard" this.name = name but could not make it work on pointers.
I guess I am still struggling with using pointers.
Addendum
I have decided to KISS and I'll be making copies of the parameters using "this" pointer.
One question remains - since all of the parameaters passed are de facto constants - harwdware pins, LCD size etc. and won't be modified by the class, why can't I just use "this>LCD_x_size" as variable instead of "this->LCD_x_size = LCD_x_size" Now the class local varaible is "LCD_x_size".
And yes, I read somewhere that using same symbols for "name" and "variable" is OK.
Confusing, but OK.
-- modified 19-Sep-15 17:56pm.
|
|
|
|