小编mar*_*olk的帖子

模板问题导致链接器错误(C++)

我几乎不知道C++模板会发生什么,但我正在尝试实现一个函数,它在向量中搜索满足给定属性的元素(在这种情况下,搜索名称给定的元素).我在.h文件中的声明如下:

template <typename T>
T* find_name(std::vector<T*> v, std::string name);
Run Code Online (Sandbox Code Playgroud)

当我编译时,我在调用函数时遇到此链接器错误:

Error   1   error LNK2019: unresolved external symbol "class Item * __cdecl find_name<class Item>(class std::vector<class Item *,class std::allocator<class Item *> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$find_name@VItem@@@@YAPAVItem@@V?$vector@PAVItem@@V?$allocator@PAVItem@@@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z) referenced in function "public: class Item * __thiscall Place::get_item(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?get_item@Place@@QAEPAVItem@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) place.obj   Program2
Run Code Online (Sandbox Code Playgroud)

同样,我是模板的新手,所以我不知道会发生什么.我通过谷歌找到的LNK2019的所有实例都没有使用正确的库,但由于这是我自己的功能,我不明白为什么会发生这种情况.

另外,一个相关的问题:是否有办法制作模板参数,以便它必须是某个类的子类,即模板?

c++ templates compiler-errors

44
推荐指数
4
解决办法
3万
查看次数

OCaml lex:根本不起作用

我在这里结束了.我无法在ocamllex工作,它让我疯了.这是我的.mll档案:

{

open Parser

}

rule next = parse
  | (['a'-'z'] ['a'-'z']*) as id { Identifier id }
  | '=' { EqualsSign }
  | ';' { Semicolon }
  | '\n' | ' ' { next lexbuf }
  | eof { EOF }
Run Code Online (Sandbox Code Playgroud)

以下是我作为输入传入的文件的内容:

a=b;
Run Code Online (Sandbox Code Playgroud)

然而,当我编译并运行该东西时,我在第一个字符上出现错误,说它无效.老实说,我不知道发生了什么,谷歌根本没有帮助我.这怎么可能呢?如你所见,我真的很难过.

编辑:

我工作了很长时间,以至于我放弃了解析器.现在这是我的主文件中的相关代码:

let parse_file filename =
  let l = Lexing.from_channel (open_in filename) in
    try
      Lexer.next l; ()
    with
      | Failure msg ->
        printf "line: %d, col: %d\n" l.lex_curr_p.pos_lnum l.lex_curr_p.pos_cnum
Run Code Online (Sandbox Code Playgroud)

打印出"line:1,col:1".

ocaml ocamllex

4
推荐指数
2
解决办法
2598
查看次数

C信号量:sem_wait抛出莫名其妙的错误

我正在研究一个我们必须使用信号量来解决的问题.我有一个数组,其中包含两个信号量,gsem并给出一定的条件调用sem_wait(&(gsem[me])),这应该等到特定进程被唤醒.但是,出于某种原因,它给了我错误Bad file descriptor.我向上看sem_wait,Open Group规范说这不是错误sem_wait导致的.这让我的整个程序变得疯狂,我不知道为什么会失败.

编辑:违规代码,根据要求.

120     sem_wait(&mutex);
121     if (inside[opp] > 0 || waiting[opp] > 0) {
122         sem_wait(&screen);
123         printf("%s %u waiting\n", names[me], t);
124         sem_post(&screen);
125         waiting[me]++;
126         sem_post(&mutex);
127         int hg = sem_wait(&(gsem[me]));
128         if (hg < 0)
129             printf("%s\n", strerror(errno));
130     } 
Run Code Online (Sandbox Code Playgroud)

我应该注意这是一项家庭作业,我们需要使用信号量.教授称之为"男女皆宜的浴室".男人和女人都可以使用它,但不能同时使用它.inside[opp]是浴室里异性的人数.waiting[opp]是等待使用它的异性的数量.screen是一个锁定访问权限的信号量stdout.该解决方案基于我们的教科书中使用传递接力棒的读者/作者问题的解决方案.

我还应该注意,我们首先必须在Ada中编写解决方案,然后将其转换为C.我的Ada解决方案有效,我逐字翻译.我确定这是一些小的语法细节.最后,我正在研究Snow Leopard,如果有帮助的话.

c semaphore

1
推荐指数
1
解决办法
3320
查看次数

标签 统计

c ×1

c++ ×1

compiler-errors ×1

ocaml ×1

ocamllex ×1

semaphore ×1

templates ×1