>
C++及编程 C++及编程 7923c

帮看看哪里有问题(kw:模板,指针)

[已注销] 2010-07-24
01 #include <algorithm>
02 #include <cstdio>
03 using namespace std;
04
05 template<typename T, int SIZE>
06 struct Dict {
07 struct Node {
08 char* key;
09 T v;
10 Node *next;
11 };
12
13 Node s[SIZE * 10];
14 int top;
15 Node* t[SIZE];
16
17 Dict() {
18 top = 0;
19 fill_n(t, SIZE, NULL);
20 }
21
22 Node* newNode() {
23 return &s[top++];
24 }
25
26 int HashString(const char *key) {
27 unsigned int h = 0;
28 while (*key) h = (h << 5) + h + *key++;
29 return h % SIZE;
30 }
31
32 T* get(const char *key) {
33 int i = HashString(key);
34 for (Node* cur = t[i]; cur != NULL; cur = cur->next)
35 if (strcmp(cur->key, key) == 0)
36 return &(cur->v);
37 return NULL;
38 }
39 void add(const char* key, const T &x) {
40 Node* cur = newNode();
41 int i = HashString(key);
42 cur->next = t[i]...43 t[i] = cur;
44 cur->v = x;
45 cur->key = new char[strlen(key) + 1];
46 strcpy( cur-><int, 100><T, Size><int, 100><int, 100><T, Size><int, 100>
01 #include <algorithm>
02 #include <cstdio>
03 using namespace std;
04
05 template<typename T, int SIZE>
06 struct Dict {
07 struct Node {
08 char* key;
09 T v;
10 Node *next;
11 };
12
13 Node s[SIZE * 10];
14 int top;
15 Node* t[SIZE];
16
17 Dict() {
18 top = 0;
19 fill_n(t, SIZE, NULL);
20 }
21
22 Node* newNode() {
23 return &s[top++];
24 }
25
26 int HashString(const char *key) {
27 unsigned int h = 0;
28 while (*key) h = (h << 5) + h + *key++;
29 return h % SIZE;
30 }
31
32 T* get(const char *key) {
33 int i = HashString(key);
34 for (Node* cur = t[i]; cur != NULL; cur = cur->next)
35 if (strcmp(cur->key, key) == 0)
36 return &(cur->v);
37 return NULL;
38 }
39 void add(const char* key, const T &x) {
40 Node* cur = newNode();
41 int i = HashString(key);
42 cur->next = t[i];
43 t[i] = cur;
44 cur->v = x;
45 cur->key = new char[strlen(key) + 1];
46 strcpy( cur->key, key);
47 }
48 };
49
50 Dict<int, 100> d;
51
52
53 int main() {
54 d.add("Tom", 2);
55 d.add("Jack",15);
56 int* r;
57 if (r = d.get("tom"))
58 printf("%d\n", *r);
59 return 0;
60 }
61
62


编译器返回信息:
|| Dict.cpp: In constructor `Dict<T, Size>::Dict() [with T = int, int Size = 100]':
Dict.cpp|51| instantiated from here
Dict.cpp|20| warning: passing NULL used for non-pointer converting 3 of `_OutputIterator std::fill_n(_OutputIterator, _Size, const _Tp&) [with _OutputIterator = Dict<int, 100>::Node**, _Size = int, _Tp = int]'
|| d:\gcc\bin\../lib/gcc/mingw32/3.4.0/../../../../include/c++/3.4.0/bits/stl_algobase.h: In function `_OutputIterator std::fill_n(_OutputIterator, _Size, const _Tp&) [with _OutputIterator = Dict<int, 100>::Node**, _Size = int, _Tp = int]':
Dict.cpp|20| instantiated from `Dict<T, Size>::Dict() [with T = int, int Size = 100]'
Dict.cpp|51| instantiated from here
gcc\include\c++\3.4.0\bits\stl_algobase.h|546| error: invalid conversion from `const int' to `Dict<int, 100>::Node*'


0
显示全文

查看更多有趣的豆瓣小组

回应 (5条) 只看楼主

  • [已注销]
    自己定
  • 17 Dict() {
    18 top = 0;
    19 fill_n(t, SIZE, static_cast<Node*>(NULL));
    20 }
  • [已注销]
    啊。。。谢谢LS,真的行了
  • bugcn
    强制类型转换,楼主想想别的办法吧。
    尽量别用这种办法。
    行倒是行了,在大型开发上这就是个不定时炸弹啊
  • [已注销]
    - - 这只是在去掉常数“0”的歧义性,这种静态转换也有问题么?
添加回应

更多相关帖子

推荐小组

值得一读

    豆瓣
    我们的精神角落
    免费下载 iOS / Android 版客户端