c++ - 之前缺少模板参数

标签 c++ linux makefile

我在强制执行我的代码时遇到问题。错误消息指示 Map.h 中“缺少模板参数”。但我确实相信 Map.h 中没有任何错误,因为它来自 Minisat,这是一个复杂的 API。所以我认为这是我的代码或 makefile 的错误。我已经尝试了很多次了,你能帮我吗?非常感谢!

The error message is:
Compiling: queryOrac/queryOrac.o
In file included from /home/parallels/Desktop/Incremental_Solver/core/SolverTypes.h:30:0,
                 from /home/parallels/Desktop/Incremental_Solver/core/Solver.h:28,
                 from /home/parallels/Desktop/Incremental_Solver/simp/SimpSolver.h:25,
                 from /home/parallels/Desktop/Incremental_Solver/queryOrac/queryOrac.cc:12:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h: In member function ‘uint32_t Minisat::Hash<K>::operator()(const K&) const’:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h:32:99: error: missing template arguments before ‘(’ token
 template<class K> struct Hash  { uint32_t operator()(const K& k)               const { return hash(k);  } };
                                                                                                   ^
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h: In member function ‘uint32_t Minisat::DeepHash<K>::operator()(const K*) const’:
/home/parallels/Desktop/Incremental_Solver/mtl/Map.h:35:103: error: missing template arguments before ‘(’ token
 template<class K> struct DeepHash  { uint32_t operator()(const K* k)               const { return hash(*k);  } };
                                                                                                       ^
make: *** [/home/parallels/Desktop/Incremental_Solver/queryOrac/queryOrac.o] Error 1

.cc 文件是(该文件是 .h 文件的实现):

#include <map>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <regex>

#include "queryOrac/queryOrac.h"
#include "incre/tools.h"
#include "incre/dict.h"
#include "simp/SimpSolver.h"
#include "utils/System.h"
#include "utils/ParseUtils.h"
#include "utils/Options.h"
#include "core/Dimacs.h"

using namespace std;
using namespace Minisat;
using namespace Incre;

Oracle::Oracle(const char * ora, const char * PI, const char * PO)
{
    cout << "a Oracle is created" << endl;
    PI_path = PI;
    PO_path = PO;
    Orac_Path = ora;
}

void Oracle::process()
{
    parse_PI();
    assign_PI();
    solve();
}

void Oracle::parse_PI() {
    cout << "reading from " << PI_path << endl;
    ifstream infile;
    infile.open(PI_path, ios::in);
    string first_line;
    string second_line;

    getline(infile, first_line);
    getline(infile, second_line);

    vector<string> name_temp;
    vector<string> value_temp;
    SplitString(first_line, name_temp, " ");
    SplitString(second_line, value_temp, " ");

    vector<string>::iterator value = value_temp.begin();
    for(vector<string>::iterator name = name_temp.begin(); name != name_temp.end(); ++name)
    {
        PI_temp.insert(pair<int, string>(varIndexDict[*name], *value));
        value++;
    }
}

void Oracle::assign_PI(){
    vector<int>::iterator position = pisIndex.begin();
    for(map<int, string>::iterator index = PI_temp.begin(); index != PI_temp.end(); ++index)
    {
        if(index->second == "1") PI_assignment_cnf.push_back(tostring(*position) + " 0\n");
        else if(index->second == "0") PI_assignment_cnf.push_back("-" + tostring(*position) + " 0\n");
        position++;
    }
    PI_assignment_cnf.insert(PI_assignment_cnf.begin(), "c this is assign_PI\n");
    cnFile += PI_assignment_cnf;
    print_vector(cnFile, "cnFile");
}


void Oracle::solve(){
    cout << " start solving" << endl;

}

我的 makefile 是:

##
##  Template makefile for Standard, Profile, Debug, Release, and Release-static versions
##
##    eg: "make rs" for a statically linked release version.
##        "make d"  for a debug version (no optimizations).
##        "make"    for the standard version (optimized, but with debug information and assertions active)

PWD        = $(shell pwd)
EXEC      ?= $(notdir $(PWD))

CSRCS      = $(wildcard $(PWD)/*.cc) 
DSRCS      = $(foreach dir, $(DEPDIR), $(filter-out $(MROOT)/$(dir)/Main.cc, $(wildcard $(MROOT)/$(dir)/*.cc)))
CHDRS      = $(wildcard $(PWD)/*.h)
COBJS      = $(CSRCS:.cc=.o) $(DSRCS:.cc=.o)

PCOBJS     = $(addsuffix p,  $(COBJS))
DCOBJS     = $(addsuffix d,  $(COBJS))
RCOBJS     = $(addsuffix r,  $(COBJS))


CXX       ?= g++ 
CFLAGS    ?= -Wall -Wno-parentheses -std=c++11 
LFLAGS    ?= -Wall

COPTIMIZE ?= -O3

CFLAGS    += -I$(MROOT) -D __STDC_LIMIT_MACROS -D __STDC_FORMAT_MACROS
LFLAGS    += -lz

.PHONY : s p d r rs clean 

s:  $(EXEC)
p:  $(EXEC)_profile
d:  $(EXEC)_debug
r:  $(EXEC)_release
rs: $(EXEC)_static

libs:   lib$(LIB)_standard.a
libp:   lib$(LIB)_profile.a
libd:   lib$(LIB)_debug.a
libr:   lib$(LIB)_release.a

## Compile options
%.o:            CFLAGS +=$(COPTIMIZE) -g -D DEBUG
%.op:           CFLAGS +=$(COPTIMIZE) -pg -g -D NDEBUG
%.od:           CFLAGS +=-O0 -g -D DEBUG
%.or:           CFLAGS +=$(COPTIMIZE) -g -D NDEBUG

## Link options
$(EXEC):        LFLAGS += -g
$(EXEC)_profile:    LFLAGS += -g -pg
$(EXEC)_debug:      LFLAGS += -g
#$(EXEC)_release:   LFLAGS += ...
$(EXEC)_static:     LFLAGS += --static

## Dependencies
$(EXEC):        $(COBJS)
$(EXEC)_profile:    $(PCOBJS)
$(EXEC)_debug:      $(DCOBJS)
$(EXEC)_release:    $(RCOBJS)
$(EXEC)_static:     $(RCOBJS)

lib$(LIB)_standard.a:   $(filter-out */Main.o,  $(COBJS))
lib$(LIB)_profile.a:    $(filter-out */Main.op, $(PCOBJS))
lib$(LIB)_debug.a:  $(filter-out */Main.od, $(DCOBJS))
lib$(LIB)_release.a:    $(filter-out */Main.or, $(RCOBJS))


## Build rule
%.o %.op %.od %.or: %.cc
    @echo Compiling: $(subst $(MROOT)/,,$@)
    @$(CXX) $(CFLAGS) -c -o $@ $<

## Linking rules (standard/profile/debug/release)
$(EXEC) $(EXEC)_profile $(EXEC)_debug $(EXEC)_release $(EXEC)_static:
    @echo Linking: "$@ ( $(foreach f,$^,$(subst $(MROOT)/,,$f)) )"
    @$(CXX) $^ $(LFLAGS) -o $@

## Library rules (standard/profile/debug/release)
lib$(LIB)_standard.a lib$(LIB)_profile.a lib$(LIB)_release.a lib$(LIB)_debug.a:
    @echo Making library: "$@ ( $(foreach f,$^,$(subst $(MROOT)/,,$f)) )"
    @$(AR) -rcsv $@ $^

## Library Soft Link rule:
libs libp libd libr:
    @echo "Making Soft Link: $^ -> lib$(LIB).a"
    @ln -sf $^ lib$(LIB).a

## Clean rule
clean:
    @rm -f $(EXEC) $(EXEC)_profile $(EXEC)_debug $(EXEC)_release $(EXEC)_static \
      $(COBJS) $(PCOBJS) $(DCOBJS) $(RCOBJS) *.core depend.mk 

## Make dependencies
depend.mk: $(CSRCS) $(CHDRS)
    @echo Making dependencies
    @$(CXX) $(CFLAGS) -I$(MROOT) \
       $(CSRCS) -MM | sed 's|\(.*\):|$(PWD)/\1 $(PWD)/\1r $(PWD)/\1d $(PWD)/\1p:|' > depend.mk
    @for dir in $(DEPDIR); do \
          if [ -r $(MROOT)/$${dir}/depend.mk ]; then \
          echo Depends on: $${dir}; \
          cat $(MROOT)/$${dir}/depend.mk >> depend.mk; \
          fi; \
      done

-include $(MROOT)/mtl/config.mk
-include depend.mk

最佳答案

您的问题是由于 using namespace std; 。永远不要这样做。

有些人说只要不在头文件中就可以,但他们错了,你的错误就是一个很好的例子。

在这种情况下,您不小心引用了 std::hash ,这是一个 class 模板,因此 <>与函数模板不同,不能省略。

关于c++ - 之前缺少模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274171/

相关文章:

c++ - 编译错误 : cast void * failed in C/C++

python - 如何在 python 中实现 makefile 风格的算法

c++ - shared_ptr 赋值 - 是否也复制了自定义删除器?

c - 管理客户端进程分配的资源

c++ - "inline"函数定义的目的是什么?

linux - Selenium 2 webdriver - 连接到 http ://localhost:7055 refused

linux - 如何将所有文件从一个目录复制到另一个目录(不包括 bash 脚本中以给定字符串开头的文件)

shell - 什么是 .shar 文件?

c++ - matlab deploytool 在尝试构建 C++ 共享库时给出错误

c++ - 奇怪的结构 - C++