Posts by C. E. Gesser • 3,783 points
54 posts
-
1
votes1
answer318
viewsA: How to return integer value in pthread?
The problem is that you are returning the address of a local variable. And this is a problem with or without threads. Since you are allocating a memory to your i (and not dislocating, which is…
-
3
votes3
answers220
viewsA: Attributeerror: 'Qstring' Object has no attribute 'strip'
The class QString has the function trimmed(), that returns a new QString with whitespace removed from the beginning and end of text. Documentation of the Pyhton version:…
-
3
votes1
answer818
viewsA: Difference between void and void*
When declaring a function in C, C++ (and other derived languages such as Java and C#) it is always necessary to declare the function return type. void is a special type to indicate that the function…
-
0
votes2
answers105
viewsA: Parameters, functions in C
The parameters of the function main allow access to arguments passed per command line to the program. For example, the sample program could be modified to receive the calculation numbers per command…
-
7
votes1
answer87
viewsA: Doubt Class C++
This is called forward declaration. It is necessary to allow cyclic references between classes. So it is possible to make the class Ble have a pointer to a class object Bla and then do the class Bla…
-
2
votes2
answers691
viewsA: How to avoid "Warning: ISO C++ forbids variable length array ːfilename' [-Wvla]" error in C++11
Two errors: The C language allows creating arrays on the variable-sized stack, but C++ does not. In C++ the size of arrays allocated to the stack must be known at build time. You’re giving delete to…
-
2
votes1
answer1884
viewsA: C++ Class Destructor Called in the middle of code
Your problem lies in the way you are accessing your object Singleton. The access function is correctly returning the object by reference but both in the function MouseCallback how much in the main…
c++answered C. E. Gesser 3,783 -
1
votes1
answer151
viewsA: Function that reads a double of a string and returns the rest of the string in C
There is the function: double strtod (const char* str, char** endptr); She converts a number double from the string passed as the first parameter, and plays in the second parameter a pointer to the…
-
1
votes2
answers91
viewsA: Project linkage problem with more than one source file
Note the command line that was used to compile: gcc ex09.c You are instructing the compiler to generate an executable from a source file only, but your project consists of more than one. Hence the…
-
1
votes2
answers2311
viewsA: How do I create a C Makefile
Your problem is not exactly in the Makefile. The file is with the correct syntax, indicating the dependencies, targets and how to generate them. The problem is in the command to generate the final…
-
2
votes2
answers507
viewsA: Doubts about the compilation and "linkage" phase of a C-Program
The internal format of the object files varies from compiler to compiler, they are not required to follow a pattern about it, but basically what they have there is the list of functions and global…
-
0
votes1
answer155
viewsA: Load operator "<<" by separating the declaration(.h) from the definition(.cpp)
You get this link error because your function is a template function. Template functions should have their implementation declared on the same build unit where they are used, it is a language…
-
2
votes1
answer95
viewsA: Why does the code generate garbage at the beginning of the vector?
Your array vtype is only size 10, but you write on it like it’s the same size as the others. The way local variables are structured, you end up writing over the memory of other arrays. In this case…
-
1
votes3
answers399
viewsA: Doubt about C pointers
This function receives from parameter a pointer to character and a quantity. It is assumed that this pointer points to an array of characters, at least equal in size to the reported amount. At the…
-
11
votes2
answers468
viewsA: Meaning of "__"
This means that if you declare a symbol (variable, function, macro, etc.) in this way, you may conflict with symbols exported by the standard compiler or library implementation. For example, you can…
-
1
votes2
answers108
viewsA: (C++) Deck: Differences between position access types
std::deque and std::vector have two ways to access elements in indexed form: By the Elder brackets [] For the function of a member at The basic difference between the two is that the function at…
-
1
votes1
answer332
viewsA: Manipulating list<int> in c++
It seems that you are implementing a graph through a list of adjacencies. My suggestion is, instead of using dynamic allocation for the list, use a std::vector. And for the list of edges of each…
-
2
votes2
answers214
viewsA: Make a reference variable point to another variable
This is not possible. Once a reference is initialized it is tied to that variable until the end of its life. So much so that you cannot create a reference without initializing: int & r; //Erro…
-
3
votes2
answers704
viewsA: Pointer operators passing by reference
The operator &, as well as several others in C++, it has more than one utility, depending on the context where it is applied: It can be used to get the address of a variable, the case you…
-
2
votes1
answer199
viewsA: Pointer to method
I have no experience with Allegro, but what you are trying to do won’t work because you are passing a pointer to function member when expected is a pointer to function free. The function is…
-
5
votes5
answers2700
viewsA: Drawing strings from a weight array
Compiling the answers so far I propose the following: #include <iostream> #include <vector> #include <string> #include <algorithm>…
-
9
votes2
answers503
viewsA: What is the error in this Code:
In a quick test note the following error messages: prog.c: In function ‘main’: prog.c:14:35: error: ‘longitude’ undeclared (first use in this function) go_south_east(&latitude, &longitude);…
-
1
votes1
answer383
viewsA: Add pointer to an Std:vector
Are you trying to add a pointer to a std::vector of "normal objects". std::vector<TimedCommand> tCommands; TimedCommand command1(...); tCommands.push_back( &command1 ); //Erro de…
-
6
votes2
answers276
viewsA: Resize into dynamic pointer turns dimension into garbage
The first point we can improve is your memory allocation. Allocating and dislocating every operation works, but is not efficient. A good solution is to allocate a larger block than needed initially,…
-
6
votes1
answer185
viewsA: Why explicitly declare the base when calling function when using templates?
This is because the compiler does not search for symbols in template parameter-dependent base classes in name resolution C++FAQ. To do this the compiler would need to wait until the template is…
-
12
votes2
answers6412
viewsA: What is the C++ copy builder for? How do I implement it?
The concept may seem strange to a Java programmer, because in this language objects are always treated with "reference semantics". When you do something like: Objeto obj1 = new Objeto(); Objeto obj2…
-
3
votes3
answers457
viewsA: Lock the window zoom
You can use the method setFixedSize(const QSize &) class QWidget to set a fixed size for a window. In practice it sets the maximum and minimum window size for the value that is passed by…
-
8
votes1
answer4156
viewsA: Command to exit the scope of a function (`break`equivalent)
break is only valid to make the execution flow out of a loop (for, while or do-while) or a switch-case. In functions you can use a return: if (!price()) { QMessageBox( ... ); return; } if (!date())…
-
10
votes2
answers455
viewsA: What is the best alternative: define, enums or variables?
If all counts are related and you want to give cohesion to them in the form of a type, enum is the best choice because they don’t allow you to mistakenly assign a wrong value to them. Pin pin1 =…
c++answered C. E. Gesser 3,783 -
4
votes3
answers3071
viewsA: Does anyone know how to insert names in alphabetical order into a c++ vector?
I suggest you take a look at the standard STL algorithms. To solve this exact problem your already exists the lower_bound #include <algorithm> void inserirNomeNaLista(std::vector<string>…
c++answered C. E. Gesser 3,783 -
1
votes1
answer143
viewsA: How to use the Qtreewidget Finditem in Pyqt ?
Try the findItems, method of QTreeWidget: tree = QtGui.QTreeWidget() #... items = tree.findItems("Teste", QtCore.Qt.MatchRecursive, 0) This produces a list of all items (QTreeWidgetItem) whose text…
-
0
votes4
answers152
viewsA: Is a compiler allowed to omit a reference member in a class?
I see problem if two different compilation drives (two .cpp files) used this one struct (via definition in a file . h, for example), and in one of them the compilation was with optimization and in…
-
4
votes5
answers453
viewsA: Is it legal to delete this in a member function?
According to this draft of standard, Section 9.3.1 (Nonstatic Member functions), item 2: If a non-static Member Function of a class X is called for an Object that is not of type X, or of a type…
-
20
votes3
answers1024
viewsA: Should I free up all the allocated memory when finishing a program?
One point to consider is that code that nowadays is a complete program can eventually be refactored to become a feature of another larger program. If the original program didn’t care about releasing…
-
13
votes1
answer2547
viewsA: When to use void* and auto*?
The two have very different uses. The void* serves primarily to pass a pointer, for any type, in a function, or to save a pointer for anything. Once a value is assigned, it can be replaced by…
-
4
votes3
answers12254
viewsA: How to initialize an array using pointers?
#include <string.h> /* ... */ int matrix[3][4]; memset(matrix, 0, 3 * 4 * sizeof(int)); The function memset receives a pointer, a value and a quantity. It will fill that amount of bytes from…
-
3
votes2
answers144
viewsA: How to make a constructor equal to Qobject?
Qobject has within it a list of children. Something like: class QObject { std::vector<QObject*> children; QObject *parent; }; The constructor basically stores the parent and adds the object to…
-
2
votes5
answers411
viewsA: How to avoid a comparison by brute force?
You can use a Map, using as Class<?> as key. For example: public class Leitor { private Map<Class<?>,String> tipoValor = new HashMap<Class<?>,String>(); {…
javaanswered C. E. Gesser 3,783 -
7
votes2
answers982
viewsA: Is it possible to perform an assignment and comparison in if clauses in Java?
It does not work directly because java does not allow automatic conversion to boolean. But you can do the following: String linha = leitor.readLine(); String saida = ""; if ( (linha =…
-
5
votes2
answers20308
viewsA: How to read a text file and take the values as integers?
#include <fstream> #include <vector> int main() { std::ifstream file("matriz.txt"); unsigned size; file >> size; if (!file) { //erro…
c++answered C. E. Gesser 3,783 -
4
votes3
answers568
viewsA: Is there an advantage to an explicit "self" instead of the implicit "this"?
Surely it would be possible that the self is an implicit parameter, such as this of other languages, but I believe this is because one of Python’s philosophies is to make things explicit. Within a…
-
2
votes5
answers1668
viewsA: How to use a language other than Javascript in your browser?
The main reason is that browsers only understand javascript anyway. And even that they do perfectly, each browser always has a specific peculiarity. IE had, I don’t know if it still has, Vbscript,…
-
2
votes3
answers85
viewsA: Allocator and placement new
In addition to what has already been said, deallocatealso has problem, should be made only one free. The parameter n is the number of elements that has been allocated, but as you make the allocation…
-
95
votes3
answers6231
viewsA: What does "??!?!" mean in C language?
It means the same thing ||. In some places people do not have all the symbols necessary to program in C on their keyboards, hence the Graphs were created: ??= = # ??/ = \ ??' = ^ ??( = [ ??) = ] ??!…
canswered C. E. Gesser 3,783 -
22
votes3
answers739
viewsA: What does ":-!!" mean in C language?
The !! is an old trick to convert numerical values into booleans. The first ! denies the value if it is 0 flipped 1, if it’s anything else 0. The second ! deny it again The next part is to use this…
-
2
votes3
answers308
viewsA: How to "create" a variable in "Runtime"?
The most efficient container for storing many objects is the std::vector. Internally it is just an array. If you will need to do searches on it, you can make it efficient using the algorithm…
-
7
votes3
answers23004
viewsA: How to pass a struct vector by reference parameter?
First you need to melt the i. After that it will be necessary a couple more parentheses: (*(dadosCliente+*i)).endereco or (dadosCliente+*i)->endereco…
-
0
votes9
answers2644
viewsA: Best practice for creating if
This will depend on case by case. If exibirAdeus for false in most cases, alternative A is more efficient, especially if the initialization of the variable is costly. Otherwise, it will make very…
-
3
votes3
answers2230
viewsA: Java shows "Type Safety: Unchecked cast from Object to Hashmap"
It’s a standard Java Warning when doing cast for a guy who uses Generics. In Eclipse, you can remove Warning by annotating the variable declaration with: @SuppressWarnings("unchecked")…
-
0
votes4
answers1341
viewsA: An algorithm for finding repeated files?
I don’t think that’s possible. You would have to compare all the files to each other, and the program’s run time would grow exponentially in relation to the number of files. You can do something…