1

的推論衝突類型我在這個問題上坐了很長一段時間。事情是,我想要一個能夠測量CPU週期和一個函數需要終止的時鐘時間的類。 問題是我想給這個類的成員函數從具有不同返回類型和參數類型的函數中計算不同的指針。 (在我的例子中是斐波那契和排序算法) 我被困住了,無法繼續。我希望你能幫助我。模板的C++錯誤和參數

Meter.h:

class Meter 
{ 
Stopwatch w; 

const char* filenameTime; 
const char* filenameCycle; 
const char* filenamePlotTime; 
const char* filenamePlotCycle; 

vector<uint64_t> t_measures; 
vector<uint64_t> c_measures; 

uint64_t t_max; 
uint64_t t_min; 
double t_mean; 

uint64_t c_max; 
uint64_t c_min; 
double c_mean; 

double t_standardDeviation; 

double c_standardDeviation; 

uint32_t numberOfTests; 

public: 

    //const char* filename; 

    Meter(const char* fileData,const char* FileCycle,const char* filePlotCycle,const    char* filePlotTime); 

    template<typename RT,typename PT> 
    void measure(uint32_t numOfTest, RT(*f)(PT arg),PT valueToTest); 

    template<typename RT,typename PT> 
    void measureAlgorithmTime(uint32_t numOfTest, RT(*f)(PT arg),PT valueToTest); 

    template<typename RT,typename PT> 
    void measureAlgorithmCycles(uint32_t numOfTest, RT(*f)(PT arg),PT valueToTest); 

    void initfileData(); 
    void initfileCycle(); 

    void printDataTime(const char* casename); 
    void printDataCycle(const char* casename); 
    void printDataToPlotTime(const char* casename,int n); 
    void printDataToPlotCycle(const char* casename, int n); 
}; 

template <typename RT, typename PT> 
void Meter::measure(uint32_t numOfTest, RT(*f)(PT arg), PT valueToTest) 
{ 
measureAlgorithmTime(numOfTest, (*f),valueToTest); 
measureAlgorithmCycles(numOfTest, (*f), valueToTest); 
} 
template <typename RT, typename PT> 
void Meter::measureAlgorithmTime(uint32_t numOfTest, RT(*f)(PT arg), PT valueToTest) 
{ 

if(numOfTest > 0)numberOfTests = numOfTest; 

t_measures = vector<uint64_t>(numberOfTests, 0); 

//do tests 
for(uint64_t i = 0; i < numberOfTests; i++) 
{ 
    w.start(); 
    (*f)(valueToTest); 
    w.stop(); 
    t_measures[i] = w.peek(); 
    w.reset(); 
} 
/* 
compute statistcs 
. 
. 
. 
*/ 
template <typename RT, typename PT> 
void Meter::measureAlgorithmCycles(uint32_t numOfTest, RT(*f)(PT arg), PT valueToTest) 
{ 

if(numOfTest > 0) numberOfTests = numOfTest; 

//build Memory for Data from Measurements 
c_measures = vector<uint64_t>(numberOfTests, 0); 



//unsigned long flags; 
uint64_t c_start, c_end, c_diff; 
unsigned cycles_low, cycles_high, cycles_low1, cycles_high1; 
//disable preemption 
//preempt_disable(); 
//disable hard interrupts 
//raw_local_irq_save(flags); 

for(uint64_t i = 0; i < numberOfTests;i++) 
{ 

    asm volatile 
    (
     "CPUID\n\t" 
     "RDTSC\n\t" 
     "mov %%edx, %0\n\t" 
     "mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx",  "%rcx", "%rdx" 
    ); 

    //call function to measure and let it do work 
    (*f)(valueToTest); 

    asm volatile 
    (
     "CPUID\n\t" 
     "RDTSC\n\t" 
     "mov %%edx, %0\n\t" 
     "mov %%eax, %1\n\t": "=r" (cycles_high1), "=r" (cycles_low1):: "%rax", "%rbx", "%rcx", "%rdx" 
    ); 

主:

#include <Meter.h> 
#include <Sorting.h> 
#include <vector> 
#include <iostream> 
using namespace std; 
int main() 
{ 
char AlgName[20]; 

cout << "Start to Measure Sorting Algorithms!" <<endl; 

uint32_t numberoftest = 10; 
int lengthOfTestVector = 10; 
vector<int> test; 
test.reserve(lengthOfTestVector); 
Sorting::addRandomNumbers(test); 
Sorting::printOut(test); 

cout << "Start to Measure InsertionSort" << endl; 

Meter InsertionSort = Meter("InsertionSort_Time","InsertionSort_Cycle", 
          "InsertionSort_Plot_Cycle","InsertionSort_Plot_Time"); 

for(uint32_t i = 0;i < numberoftest;i++) 
{ 
    InsertionSort.measure(numberoftest,*Sorting::insertionSort<int>,test); 
    InsertionSort.printDataTime(AlgName); 
    InsertionSort.printDataCycle(AlgName); 
    InsertionSort.printDataToPlotCycle(AlgName,i); 
    InsertionSort.printDataToPlotTime(AlgName,i); 
} 

cout << "finished to Measure InsertionSort" << endl; 


} 

的2個功能我想給儀表::措施是:

  • uint64_t中斐波那契:: calculateNthFibonacciNumber(uint32_t的n) 和
  • void Sorting :: inse rtionSort(矢量& toSort)

我編譯這與gcc和我自己的生成文件。

的Makefile:

#makros 
CXX = g++ 
CXXFLAGS = -std=c++0x -Wall -O -c 
FLAGSFOROBJECTS = -o [email protected] 
#-I/usr/src/linux-headers-3.13.0-32/include/asm-generic 
CXXFLAGS_GTEST = -I/home/maximilian/gtest-1.7.0/include 
CXXFLAGS_INCLUDE = -I/home/maximilian/Documents/AlgorithmEngineering/includes 
#LDFLAGS = -L/usr/lib/libgtest.a -pthread 
LDFLAGS = /home/maximilian/gtest-1.7.0/lib/.libs/libgtest.a -pthread 
OBJECTS = objects/ 
MAINS = main/ 

$(OBJECTS)Sorting.o: src/Sorting.cpp includes/Sorting.h 
    $(CXX) $(CXXFLAGS) $(CXXFLAGS_INCLUDE) src/Sorting.cpp $(FLAGSFOROBJECTS) 

$(OBJECTS)Stopwatch.o: test/Stopwatch.cpp includes/Stopwatch.h 
    $(CXX) $(CXXFLAGS) $(CXXFLAGS_INCLUDE) test/Stopwatch.cpp $(FLAGSFOROBJECTS) 

$(OBJECTS)Meter.o: test/Meter.cpp includes/Meter.h 
    $(CXX) $(CXXFLAGS) $(CXXFLAGS_INCLUDE) test/Meter.cpp $(FLAGSFOROBJECTS) 

$(OBJECTS)Sorting_meter.o: $(MAINS)Sorting_meter.cpp 
    $(CXX) $(CXXFLAGS) $(CXXFLAGS_INCLUDE) $(MAINS)Sorting_meter.cpp $(FLAGSFOROBJECTS) 
Sorting_meter: $(OBJECTS)Sorting_meter.o $(OBJECTS)Sorting.o $(OBJECTS)Meter.o  $(OBJECTS)Stopwatch.o 
    $(CXX) -o Sorting_meter $(OBJECTS)Sorting_meter.o $(OBJECTS)Sorting.o $(OBJECTS)Meter.o $(OBJECTS)Stopwatch.o $(LDFLAGS) 

錯誤是:

g++ -std=c++0x -Wall -O -c -I/home/maximilian/Documents/AlgorithmEngineering/includes main/Sorting_meter.cpp -o objects/Sorting_meter.o 
main/Sorting_meter.cpp: In function ‘int main()’: 
main/Sorting_meter.cpp:26:77: error: no matching function for call to  ‘Meter::measure(uint32_t&, void (&)(std::vector<int>&), std::vector<int>&)’ 
    InsertionSort.measure(numberoftest,*Sorting::insertionSort<int>,test); 
                     ^
main/Sorting_meter.cpp:26:77: note: candidate is: 
In file included from main/Sorting_meter.cpp:1:0: 
/home/maximilian/Documents/AlgorithmEngineering/includes/Meter.h:55:14: note:  template<class RT, class PT> void Meter::measure(uint32_t, RT (*)(PT), PT) 
    void measure(uint32_t numOfTest, RT(*f)(PT arg),PT valueToTest); 
     ^
/home/maximilian/Documents/AlgorithmEngineering/includes/Meter.h:55:14: note: template  argument deduction/substitution failed: 
main/Sorting_meter.cpp:26:77: note: deduced conflicting types for parameter ‘PT’  (‘std::vector<int>&’ and ‘std::vector<int>’) 
    InsertionSort.measure(numberoftest,*Sorting::insertionSort<int>,test); 
                     ^
make: *** [objects/Sorting_meter.o] Error 1 

我用盡了一切辦法,以避免錯誤,但現在我不知道,我希望你能幫助我。

在此先感謝!

回答

2
template <typename RT, typename PT> 
void Meter::measure(uint32_t numOfTest, RT(*f)(PT arg), PT valueToTest) 
{ 
    // […] 
} 

PT從第二和第三個參數中推導出來。在main我們已經有了

InsertionSort.measure(numberoftest, *Sorting::insertionSort<int>, test); 

注分別類型第二和第三個參數,:

  • test會導致PT被推導出vector<int>

  • 我們看不到Sorting::insertionSort的聲明,但我認爲它以vector<T>&爲參數。因此PT將被推斷爲vector<int>&,這不是我們以前的扣除產生的。 (星號是大概是不必要的,因爲它會自動衰減專業化一個函數指針並隨後解引用它)

解決,通過使所述第三參數的非推導出上下文,或使用第二個模板參數。例如。

​​
+0

嘿科倫坡,謝謝你的回答! 你能解釋一下你的意思嗎? 我對C++相當陌生。 – Schweigerama 2014-12-07 12:16:36

+0

@Schweigerama [Here](http:// stackoverflow。COM/Q /3647361分之25245453) – Columbo 2014-12-07 12:17:51

+0

和分類::插入排序是 模板(類型名T) 無效排序::插入排序(矢量&toSort) {} – Schweigerama 2014-12-07 12:17:58