-3

I'm using:

g++ -std=c++0x

compiler I'm developing some project using Cudd package http://vlsi.colorado.edu/~fabio/CUDD/ and I wanted to cache some results so here is my cache c++ code

lrucache.h

#ifndef __LRUCACHE_H_
#define __LRUCACHE_H_

#include <iostream>//=> line 4
#include <vector>
#include <unordered_map>
#include "cudd.h"

using namespace std;


struct LRUCacheEntry
{
    unsigned int key;
    DdNode * data;
    LRUCacheEntry* prev;
    LRUCacheEntry* next;
};


class LRUCache
{
private:
    std::unordered_map<unsigned int, LRUCacheEntry *> _mapping;
    vector<LRUCacheEntry *> _freeEntries;
    LRUCacheEntry *head;
    LRUCacheEntry *tail;
    LRUCacheEntry *entries;
    DdManager *nfaobdd_manager;

public:
    LRUCache(size_t size, DdManager *nfaobdd_manager_ref);
    ~LRUCache();

    void put(unsigned int key, DdNode * data);

    DdNode * get(unsigned int key);

private:
    void detach(LRUCacheEntry* node);

    void attach(LRUCacheEntry* node);

};

#endif /*__LRUCACHE_H_*/

lrucache.c

#include <lrucache.h> //=>line 1


LRUCache::LRUCache(size_t size, DdManager *nfaobdd_manager_ref)
{

    entries = new LRUCacheEntry[size];

    for (int i=0; i<size; i++)
        _freeEntries.push_back(entries+i);

    head = new LRUCacheEntry;
    tail = new LRUCacheEntry;
    head->prev = NULL;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;

    nfaobdd_manager = nfaobdd_manager_ref;
}

LRUCache::~LRUCache()
{
    LRUCacheEntry* node = head->next;
    while(node!=tail){

            Cudd_RecursiveDeref(nfaobdd_manager,node->data);
    }
    delete head;
    delete tail;
    delete [] entries;
}

void LRUCache::put(unsigned int key, DdNode * data)
{   
    Cudd_ref(data);

    LRUCacheEntry* node = _mapping[key];
    if(node)
    {
        // refresh the link list
        detach(node);
        node->data = data;
        attach(node);
    }
    else{
        if ( _freeEntries.empty() )
        {
            node = tail->prev;
            detach(node);
            _mapping.erase(node->key);
            Cudd_RecursiveDeref(nfaobdd_manager,node->data);
            node->data = data;
            node->key = key;
            _mapping[key] = node;
            attach(node);
        }
        else{
            node = _freeEntries.back();
            _freeEntries.pop_back();
            node->key = key;
            node->data = data;
            _mapping[key] = node;
            attach(node);
        }
    }
}


DdNode * LRUCache::get(unsigned int key)
{
    LRUCacheEntry* node = _mapping[key];
    if(node)
    {
        detach(node);
        attach(node);
        return node->data;
    }
    else return NULL;
}


void LRUCache::detach(LRUCacheEntry* node)
{
    node->prev->next = node->next;
    node->next->prev = node->prev;
}
void LRUCache::attach(LRUCacheEntry* node)
{
    node->next = head->next;
    node->prev = head;
    head->next = node;
    node->next->prev = node;
}

nfaobdd.h

#ifndef __NFAOBDD_H_
#define __NFAOBDD_H_
.
.
#include <lrucache.h>//=>line 69
.
.
class NFAOBDD{
.
.
LRUCache *lrucache;
.
}
#endif /*__NFAOBDD_H_*/

nfaobdd.c

#include "nfaobdd.h" //=>line 10
.
.
lrucache = new LRUCache(100, manager);
.
.
/*character is of type unsigned int*/
DdNode * char_BDD = lrucache->get(character);
if(!char_BDD){
    /*calculate the value for char_BDD*/
    char_BDD = convert_decimal_to_BDD(character, manager, input_char); 
    /*cache the calculated value*/
    lrucache->put(character, char_BDD);
}
.
.
delete lrucache;

When I use this cache in my project I get the following error that I can't understand. I appreciate any help

In file included from /usr/include/c++/4.6/ios:45:0,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from /home/nadaossama/Dropbox/Master_Project/regex/regex/lrucache.h:4,
                 from nfaobdd.h:69,
                 from nfaobdd.c:10:
/usr/include/c++/4.6/bits/basic_ios.h: At global scope:
/usr/include/c++/4.6/bits/basic_ios.h:193:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h:194:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h: In member function ‘std::basic_ios<_CharT, _Traits>::operator void*() const’:
/usr/include/c++/4.6/bits/basic_ios.h:114:22: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h:114:22: error: expected ‘;’ before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h:114:22: error: expected primary-expression before ‘)’ token
/usr/include/c++/4.6/bits/basic_ios.h:114:29: error: expected primary-expression before ‘?’ token
/usr/include/c++/4.6/bits/basic_ios.h: In member function ‘bool std::basic_ios<_CharT, _Traits>::operator!() const’:
/usr/include/c++/4.6/bits/basic_ios.h:118:22: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h:118:22: error: expected ‘;’ before ‘{’ token
/usr/include/c++/4.6/bits/basic_ios.h:118:22: error: expected primary-expression before ‘)’ token
In file included from /usr/include/c++/4.6/ostream:588:0,
                 from /usr/include/c++/4.6/iostream:40,
                 from /home/nadaossama/Dropbox/Master_Project/regex/regex/lrucache.h:4,
                 from nfaobdd.h:69,
                 from nfaobdd.c:10:
/usr/include/c++/4.6/bits/ostream.tcc: In member function ‘std::basic_ostream<_CharT, _Traits>::pos_type std::basic_ostream<_CharT, _Traits>::tellp()’:
/usr/include/c++/4.6/bits/ostream.tcc:244:15: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/ostream.tcc:244:15: error: expected ‘)’ before ‘{’ token
/usr/include/c++/4.6/bits/ostream.tcc: In member function ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::seekp(std::basic_ostream<_CharT, _Traits>::pos_type)’:
/usr/include/c++/4.6/bits/ostream.tcc:265:15: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/ostream.tcc:265:15: error: expected ‘)’ before ‘{’ token
/usr/include/c++/4.6/bits/ostream.tcc: In member function ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::seekp(std::basic_ostream<_CharT, _Traits>::off_type, std::ios_base::seekdir)’:
/usr/include/c++/4.6/bits/ostream.tcc:297:15: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/ostream.tcc:297:15: error: expected ‘)’ before ‘{’ token
In file included from /usr/include/c++/4.6/istream:859:0,
                 from /usr/include/c++/4.6/iostream:41,
                 from /home/nadaossama/Dropbox/Master_Project/regex/regex/lrucache.h:4,
                 from nfaobdd.h:69,
                 from nfaobdd.c:10:
/usr/include/c++/4.6/bits/istream.tcc: In member function ‘std::basic_istream<_CharT, _Traits>::pos_type std::basic_istream<_CharT, _Traits>::tellg()’:
/usr/include/c++/4.6/bits/istream.tcc:829:19: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/istream.tcc:829:19: error: expected ‘)’ before ‘{’ token
/usr/include/c++/4.6/bits/istream.tcc: In member function ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::seekg(std::basic_istream<_CharT, _Traits>::pos_type)’:
/usr/include/c++/4.6/bits/istream.tcc:859:19: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/istream.tcc:859:19: error: expected ‘)’ before ‘{’ token
/usr/include/c++/4.6/bits/istream.tcc: In member function ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::seekg(std::basic_istream<_CharT, _Traits>::off_type, std::ios_base::seekdir)’:
/usr/include/c++/4.6/bits/istream.tcc:898:19: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.6/bits/istream.tcc:898:19: error: expected ‘)’ before ‘{’ token
user3060396
  • 163
  • 1
  • 1
  • 5
  • Would you be so kind as to point out on which line(s) the error(s) occur? – Tim Apr 29 '14 at 12:47
  • 1
    And if you edit the question to remove the unrelated code and instead add the (relevant parts of the) `nfaobdd.h` and `nfaobdd.c` files we might even be able to help you. – Some programmer dude Apr 29 '14 at 12:47
  • We need the first 11 lines of `"nfaobdd.c"` and the first 70 lines of `"nfaobdd.h"` to determine the typig error. – Deduplicator Apr 29 '14 at 12:52
  • your destructor has undefined behaviour. – molbdnilo Apr 29 '14 at 12:59
  • sorry for the long code, the line pointed to in the error "nfaobdd.c:10" just includes the nfaobdd.h & line "nfaobdd.h:69" includes the lrucache.h – user3060396 Apr 29 '14 at 13:10
  • @molbdnilo could you please illustrate more – user3060396 Apr 29 '14 at 13:13
  • @user3060396 You're deleting objects, then using them. You should do it the other way around. – molbdnilo Apr 29 '14 at 13:28
  • 1
    Is there nothing between line 2 and line 69 in "nfaodd.h"? – molbdnilo Apr 29 '14 at 13:32
  • between line 2 and line 69 in nfaobdd.h there are other packages included – user3060396 Apr 29 '14 at 13:34
  • The problem lies between the first line and line 69 in the header file. Not after line 69. If there are other of your header files being included, you need to check them to see if any of them is missing a semicolon (which is the most likely error) at the end of a structure of function declaration. – Some programmer dude Apr 29 '14 at 13:34
  • This error message (typically) means the compiler finds a type (class, ...) that was not declared before usage. The prime suspects are DdNode and DdManager. Check carefully if your #includes actually declare them (and define them before using any member). – gnometorule Apr 29 '14 at 13:39
  • but whenever I remove the lrucache.h from the nfaobdd.h and every thing related to it from the nfaobdd.c and nfaobdd.h the code compiles perfectly, so I think if there was any problem in the rest of the header files included in the nfaobdd.h the error should also appear even if the lrucache.h is removed – user3060396 Apr 29 '14 at 13:40
  • I think you are not replying to my comment, but what you say makes it even more likely that you use either/both of the above-mentioned classes, but they are not declared (defined) before you use them (not in the files you included). The file to check for issues is likely cudd.h. – gnometorule Apr 29 '14 at 13:50
  • @gnometorule the DdNode and DdManager are defined in cudd.h which is already included. so all classes are defined – user3060396 Apr 29 '14 at 13:57
  • @molbdnilo thanks I fixed it. But this didn't solve the problem too – user3060396 Apr 29 '14 at 13:59
  • @gnometorule you are right, cudd package require including two header file to be able to use it cudd.h and util.h Thanks for your help – user3060396 Apr 29 '14 at 14:19

1 Answers1

0

Cudd package requires at least #include "util.h" and #include "cudd.h" By adding #include "util.h" to the lrucahe.h the problem was solved

Thank you all for your help

user3060396
  • 163
  • 1
  • 1
  • 5