【问题标题】:Cannot find why g++ think a function is not declared in the scope找不到为什么 g++ 认为函数未在范围内声明
【发布时间】:2011-07-08 20:44:48
【问题描述】:

我无法弄清楚为什么 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);
}

【问题讨论】:

  • 您没有显示 .cpp。有问题。顺便说一句,在标题中定义函数几乎总是错误的。
  • 这不是你的正确标题,因为它不包含memAccessUpdate
  • memHashT.h 在哪里?我认为这不是我们正在查看的文件,但这是所有错误所在。
  • 您的编码似乎有些问题。查看 gcc 错误消息中的奇怪字符。 In function âvoid←这不正常。
  • @ulidtko:其实这并不罕见。 GCC 使用了“智能引号”,在从控制台到剪贴板到浏览器到 StackOverflow 的传输过程中丢失了。

标签: c++ c function header scope


【解决方案1】:

这行几乎肯定是错误的:

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

它是mList**,所以它应该是sizeof(mList*) 乘以一个数字,而不是sizeof(mList) 乘以一个数字。

这里有一个明显的语法错误:

   for(i=0;ishared = false;

看起来毫无意义,因为 for 的评估部分中的 ishared=false 意味着循环将永远不会运行。

顺便问一下为什么用g++编译C代码?

【讨论】:

  • 是的,代码显示不正确。我使用 g++ 的原因是因为最终头文件将与 C++ 程序一起使用,所以我想确保它可以被编译。
【解决方案2】:

您在声明函数之前使用它们。例如 getHTElement 使用 getHTIndex 直到之后才声明。

一般来说,在标题中定义函数几乎总是错误的。在标头中声明它们并在源文件中定义它们。

(您的测试用例中还有许多错误告诉我您发布的代码不是您正在使用的代码,但足以推断出上述内容。)

【讨论】:

  • 阅读:“你的睾丸也有很多错误......” 我需要更多的睡眠。 ~_~
  • 真的吗?对不起,我是个菜鸟,我觉得没关系。我总是首先在 Java 和 C# 中声明我的 main 函数,我猜我没有意识到 C 和 C++ 的顺序很重要......
  • @technaj:是的! Java 和 C# 是更高级别的。 C 和 C++ 编译器从上到下解析,在抽象层次结构中更接近最低级别的机器代码,即 100% 过程指令。
【解决方案3】:
for(i=0;ishared = false;

此行不完整。在 Vim 中,后面的每个大括号都是红色的。

【讨论】:

  • 是的,它在页面上显示错误,很抱歉现在应该修复它。这不是错误:(。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多