【问题标题】:Error with typedef and function declarationtypedef 和函数声明错误
【发布时间】:2011-03-04 15:42:34
【问题描述】:

我不明白为什么当我只是声明一个变量时,它会告诉我有一个无效的函数声明。另外,我不明白为什么我的 typedef 不起作用。

用户名@Server 388> g++ -Wall -pthread testHashT.cpp -o testHashT memHashT.h:22:错误:无效的函数声明 memHashT.h:在函数“void memAccessUpdate(void*, unsigned int, pthread_t, bool)”中: memHashT.h:114:错误:无法在赋值中将âmList*â转换为âlinkedMlist*â memHashT.h:119: 错误:无法在赋值中将 âlinkedMlist*â 转换为 âmList*â memHashT.h:128:错误:“countWS”未在此范围内声明 memHashT.h:133:错误:“countRA”未在此范围内声明
Username@Server 389> cat memHashT.h
//Basic hash table for memory addresses, recommended size for program running is table indexed by a prime ~81281


/************************************************************
* Structure of table:
* Hash table indexed by memory address
* at each entry a vector made of arrays with size 2 of void pointers
* each vector entry will have the unique memory address and a pointer to a vector
* the vector it points to will contain a list of all threads that accessed that location
*
* Overall:(currently being changed to HT=>LL=>LL
* Hash Table => Vector => Vector Containing threads that accessed a given memory location
*************************************************************/

#include <pthread.h>//same as bellow
#include <stdlib.h>//watch out in case actual function contains this
//using namespace std;

//Global var
unsigned int tableSize; //note this is best if it is a prime number
unsigned long long countWA;
unsigned long long countRA:
unsigned long long countWS;
unsigned long long countRS;
//Linked Lists (thread, then mem)
//added all information in T_list to the M list, can be deleted
/*struct linkedT_list {
    int threadID;
    struct linkedT_list * nextT;
};
typedef struct linkedT_list tList;
tList * currT, * headT;*/


//For memory addresses
struct linkedM_list {
    void * memAddr;

    //Details
        bool shared;
        pthread_t  prevThread;
        unsigned long long rCounter;
        unsigned long long wCounter;
    //End Details
    struct linkedMlist * nextM;
};
typedef struct linkedM_list mList;
//mList * currM, * headM;

mList ** hashTable;

//computes the index of the hash table
        //made its own function in case future implementation need to change how indexing is set up
unsigned int getHTIndex (void * arg){
    return (unsigned int) ((unsigned long long)arg%tableSize);
}
//returns pointer to mList containing information
        //NOT CORRECTLY IMPLEMENTED YET, NEED TO FIND RIGHT MEM ADDRESS, NOT JUST TOP MEM
mList * getHTElement(void * arg){
    mList * tempM;
    //tList * tempT = NULL;
    unsigned int index = getHTIndex(arg);
    tempM = hashTable[index];
    //tempT = tempM->threadList;
    return tempM;
}
//remove a element

void removeHTElement(void * memArg){
    //note no garbage collection yet
    ;
}

//returns the number of threads to access a memery location
int tLength(void * arg){
    return -1;
}
//Create the hash table
int createHashTable(unsigned int num) {
    tableSize =  num;
    hashTable = (mList **) malloc(sizeof(mList) * tableSize);
   if (hashTable == NULL)
   {
        return 0;
        //printf("Error: Memory could not be allocated");
   }
   else {
        unsigned int i;

        for(i=0;i<tableSize;i++)
        {
            hashTable[i]=NULL;
        }
   }
    return 1;
}

void destroyHashTable(){
    free(hashTable);
}

//adds a element to the hash table
void memAccessUpdate(void * memArg, unsigned int thread, pthread_t thread_id,  bool writeAccess){
    mList * headM = getHTElement(memArg);
    mList * currM;
    if (headM == NULL)
    {//then create and new mList
        currM = (mList *)malloc(sizeof(mList));
        //initialize values
        currM->shared = false;
        currM->prevThread = thread_id;
        currM->rCounter = 0;
        currM->wCounter = 0;
        currM->nextM = hashTable[getHTIndex(memArg)];
        hashTable[getHTIndex(memArg)] = currM;
    }
    else {//change details in linked list and global var
        //headM->nextM = (mList *)malloc(sizeof(mList));
        currM = headM->nextM;
        if (thread_id != currM->prevThread){
            currM->shared = true;
            currM->prevThread = thread_id;
        }
        if(writeAccess)
        {
            countWA++;
            if(currM->shared){
                countWS++;
            }
            currM->wCounter++;
        }
        else{//mem read
            countRA++;
            if(currM->shared){
                countRS++;
            }
            currM->rCounter++;
        }
    }
    //if (stuff) //possibly don't need
    //else
   // head = hashTable[index]; //note may be null
   // curr = (mList *)malloc(sizeof(mList));
   // curr->
}

我很困惑为什么会出现这些错误。任何帮助表示赞赏。 (我知道在标头中声明函数不是一个好习惯,但现在我只是想更正我的语法)。

【问题讨论】:

  • 虽然不是您问题的答案,但是(假设这是来自 MacOS X 终端)如果您设置 Terminal->Preferences->Settings->Advanced->Character encodingUnocode (UTF-8),编译器的输出将更具可读性,因为 â 将被打印为引号字符。

标签: c++ c function declaration


【解决方案1】:

知道了!我花了一段时间,我的视力是什么...仔细看第22行的最后一个字符:-)

另外,决定您是想将其命名为linkedMlist 还是linkedM_list。 (那个眼睛没那么难看。)

【讨论】:

  • 虽然对编译器来说它看起来像一个函数声明,但我不知道!
  • @Charles: 必须是 C++ 的东西 :)
【解决方案2】:

struct linkedMlist 没有定义或声明,据我所知

struct linkedM_list {
    void * memAddr;

    //Details
        bool shared;
        pthread_t  prevThread;
        unsigned long long rCounter;
        unsigned long long wCounter;
    //End Details
    struct linkedMlist * nextM;                // <===== HERE
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多