- FatCow
- Apr 22, 2002
-
I MAP THE FUCK OUT OF PEOPLE
|
I'm using a stl set to hold multiple instances of a class. Is it considered poor form to make things 'mutable' that aren't used in the comparison operator to get around the implicit cast to const?
|
#
¿
Mar 13, 2008 02:49
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 7, 2024 21:45
|
|
- FatCow
- Apr 22, 2002
-
I MAP THE FUCK OUT OF PEOPLE
|
Drx Capio posted:
...But yeah, it's kind of bad form because mutable usually designates pretty specific functionality like access statistics and caching. The general rule is that the (visible) state of the object should never change in a const method.
That Turkey Story posted:
It's kind of hackish, have you considered using a map instead or are you unable to partition the type? If you have Boost you might also want to consider using a Multi-Index Container and modify the value with its "modify" function template.
Thanks. I switched to a map and pulled the items used in the index since they aren't needed other then for searching on.
FatCow fucked around with this message at 23:44 on Mar 13, 2008
|
#
¿
Mar 13, 2008 23:42
|
|
- FatCow
- Apr 22, 2002
-
I MAP THE FUCK OUT OF PEOPLE
|
This one had me stumped for a hour or so at work before I got pulled away. When I compile my application everything works fine. When it tries to link I get " undefined reference" to everything in the threadqueue class (Quick wrapper around STL queues to make it threadsafe).
There has to be something so obviously stupid here but I can't see it.
threadqueue.h
code:#ifndef _THREADQUEUE_H_
#define _THREADQUEUE_H_
#include <pthread.h>
#include <queue>
using namespace std;
template <class T>
class threadqueue {
private:
pthread_mutex_t mutex;
queue<T> data;
public:
threadqueue();
~threadqueue ();
void push ( const T& );
T pop ();
int size ();
};
#endif
threadqueue.cpp
code:#include <pthread.h>
#include <queue>
using namespace std;
#include "threadqueue.h"
template <class T> threadqueue<T>::threadqueue () {
pthread_mutex_init (&mutex, NULL);
}
template <class T> void threadqueue<T>::push( const T& x ) {
pthread_mutex_lock(&mutex);
data.push(x);
pthread_mutex_unlock(&mutex);
}
template <class T> T threadqueue<T>::pop( ) {
pthread_mutex_lock(&mutex);
T x;
x = data.front();
data.pop();
pthread_mutex_unlock(&mutex);
return x;
}
template <class T> int threadqueue<T>::size () {
return data.size();
}
GCC output
code:g++ -o threadqueue.o -c -g -O0 -Wall -Ulinux -Dlinux=linux -I/usr/local/ssl/include -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/lib/perl5/5.8.8/i686-linux/CORE threadqueue.cpp
g++ -o acdtool -Wall threadqueue.o main.o acdtool.o listener.o dialog.o collector.o snmp.o util.o dispatch.o ldr_pcap.o ./libs/libosip2.a ./libs/libosipparser2.a -lpcap -lpthread `net-snmp-config --agent-libs`
acdtool.o: In function `acdtool::main_loop()':
/home/me/acdtool.cpp:86: undefined reference to `threadqueue<acd_message*>::size()'
/home/me/acdtool.cpp:91: undefined reference to `threadqueue<acd_message*>::pop()'
acdtool.o: In function `acdtool::addMessage(acd_message*)':
/home/me/acdtool.cpp:73: undefined reference to `threadqueue<acd_message*>::push(acd_message* const&)'
acdtool.o: In function `~acdtool':
/home/me/acdtool.cpp:68: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
/home/me/acdtool.cpp:68: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
/home/me/acdtool.cpp:68: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
/home/me/acdtool.cpp:68: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
acdtool.o: In function `acdtool':
/home/me/acdtool.cpp:21: undefined reference to `threadqueue<acd_message*>::threadqueue()'
/home/me/acdtool.cpp:36: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
/home/me/acdtool.cpp:21: undefined reference to `threadqueue<acd_message*>::threadqueue()'
/home/me/acdtool.cpp:36: undefined reference to `threadqueue<acd_message*>::~threadqueue()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
$ nm threadqueue.o
$
$ strip threadqueue.o
$ -rw-rw-r-- 1 me me 432 Dec 9 04:26 threadqueue.o
|
#
¿
Dec 9, 2008 05:28
|
|
- FatCow
- Apr 22, 2002
-
I MAP THE FUCK OUT OF PEOPLE
|
That did it, what a stupid mistake.
|
#
¿
Dec 9, 2008 05:39
|
|