【发布时间】:2023-03-25 17:24:01
【问题描述】:
好的,我应该在链接时定义一个变量 HASH_TABLE_SIZE,所以我会把它放在我的 makefile 中。
我的Makefile如下:
1 CC = g++
2 CFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -D HASH_TABLE_SIZE=10
3 LDFLAGS = -lrt
4
5 myhash : main.o hash.o hash_function.o
6 $(CC) $(LDFLAGS) main.o hash.o hash_function.o -o myhash
7
8 main.o : main.cpp hash.h
9 $(CC) $(LDFLAGS) main.cpp
10
11 hash.o : hash.cpp hash.h
12 $(CC) $(LDFLAGS) hash.cpp
13
14 hash_function.o : hash_function.cpp hash.h
15 $(CC) $(LDFLAGS) hash_function.cpp
16
17 clean :
18 rm *.o myhash
我的 Makefile 对我来说似乎是正确的,我输入 -D HASH_TABLE_SIZE=10。 但是,当我做时,我得到了这个错误:
In file included from main.cpp:3:0:
hash.h:24:27: error: 'HASH_TABLE_SIZE' was not declared in this scope
list<string> hashTable[HASH_TABLE_SIZE];
我的Hash.h文件如下:
1 /* This assignment originated at UC Riverside. The hash table size
2 should be defined at link time. Use -D HASH_TABLE_SIZE=X */
3
4 #ifndef __HASH_H
5 #define __HASH_H
6
7 #include <string>
8 #include <list>
9
10 using namespace std;
11
12 class Hash {
13
14 public:
15 Hash();
16 void remove(string word);
17 void print();
18 void processFile(string fileName);
19 bool search(string word);
20 void output(string fileName);
21 void printStats();
22
23 private:
24 list<string> hashTable[HASH_TABLE_SIZE];
25 int collisions;
26 int longestList;
27 double avgLength;
28
29 private:
30 int hf(string ins);
31 double newAvgListLen;
32
33 // put additional variables/functions below
34 // do not change anything above!
35
36 };
37
38 #endif
为什么会这样?任何帮助将不胜感激。
【问题讨论】:
-
mkfile 的构建行上的 CFLAGS 在哪里?
-
该错误是编译器错误,发生在链接步骤开始之前;编译器抱怨您在源代码中留下了占位符
HASH_TABLE_SIZE。您确定要在链接时而不是编译时进行分配吗?
标签: c++ arrays linker makefile hashtable