簡體   English   中英

無法找到為什么g ++認為函數未在范圍中聲明

[英]Cannot find why g++ think a function is not declared in the scope

我不知道為什么g ++會出現以下錯誤。 我相信所有函數都已正確聲明並且大括號匹配。

(testHashT.cpp僅具有標題和一個空的main)


USERNAME@SERVER 391> g++ -Wall -pthread testHashT.cpp -o testHashT
memHashT.h:22: error: invalid function declaration
memHashT.h: In function âvoid memAccessUpdate(void*, unsigned int, pthread_t, bool)â:
memHashT.h:78: error: âgetHTElementâ was not declared in this scope
memHashT.h:88: error: âgetHTIndexâ was not declared in this scope
memHashT.h:93: error: cannot convert âlinkedMlist*â to âmList*â in assignment
memHashT.h:102: error: âcountWSâ was not declared in this scope
memHashT.h:107: error: âcountRAâ was not declared in this scope
memHashT.h: In function âmList* getHTElement(void*)â:
memHashT.h:133: error: âgetHTIndexâ was not declared in this scope

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;
//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->
}

//remove a element

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

//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;
}

//returns the number of threads to access a memery location
int tLength(void * arg){
    return -1;
}

//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);
}

在聲明函數之前,先要使用它們。 例如, getHTElement使用getHTIndex ,直到之后才聲明。

通常,在標頭中定義函數幾乎總是錯誤的。 在標頭中聲明它們,並在源文件中定義它們。

(您的測試用例中還存在許多錯誤,這告訴我,您發布的代碼不是您正在使用的代碼,但這足以推斷出以上內容。)

for(i=0;ishared = false;

這行是不完整的。 在Vim中,以下所有大括號均為紅色。

這行幾乎可以肯定是錯誤的:

   hashTable = (mList **) malloc(sizeof(mList) * tableSize);

它是mList**因此應為sizeof(mList*)乘以數字,而不是sizeof(mList)乘以數字。

這里有一個明顯的語法錯誤:

   for(i=0;ishared = false;

而且看起來毫無意義,因為ishared=false的評估部分中的ishared=false意味着循環永遠不會運行。

順便問一下,為什么要用g ++編譯C代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM