2011-10-05 5 views
0

나는 컴파일시 다음과 같은 오류를 받고 있어요 : 컴파일러 오류 템플릿 함수를 호출하는 동안

 Compilation started at Wed Oct 5 03:05:32 
|make -k proj1 
|g++  proj1.cc -o proj1 
|proj1.cc: In function ‘int main()’: 
|proj1.cc:75:13: error: no matching function for call to ‘getData()’ 
|proj1.cc:75:13: note: candidate is: 
|proj1.cc:46:6: note: template<class T> void getData(Vector<T>&, int&) 
|proj1.cc:80:16: error: no matching function for call to 
‘computeSum()’ 
|proj1.cc:80:16: note: candidate is: 
|proj1.cc:28:6: note: template<class T> void computeSum(Vector<T>, 
int, T&, bool&) 
|proj1.cc:83:9: error: ‘success’ was not declared in this scope 
|proj1.cc:84:27: error: ‘total’ was not declared in this scope 
make: *** [proj1] Error 1 
    Compilation exited abnormally with code 2 at Wed Oct 5 03:05:33 

내가 내 템플릿 호출 간단하지 있습니까

#include <std_lib_facilities.h>                                             
#include <vector> 
#include <string> 
#include <iostream> 

class T 
{ 
public:                                  
    void computeSum(vector<T> in, int n, T &out, bool &success); 
    void getData(vector<T> &data, int &howMany); 
}; 

template <class T> 
// void computeSum(vector<T> data, int howMany, T& out, bool& success)                                   
void computeSum(vector<T> data, int n, T &out, bool &success) 
{ 

    if (n < data.size()){ 
     success = true; 
     int i = 0; 
     while (i<n){ 
      out = out + data[i]; 
      ++i; 
     } 
    } else { 
     success = false; 
     cerr << "You can not request to sum up more numbers than there are.\n"; 
    } 

} 

template <class T> 
void getData(vector<T> &data, int &howMany) 
{ 
    cout << "Please insert the data:\n";                                               
    T n; 

    do{ 
     cin >> n; 
     data.push_back(n); 
    } while (n<howMany); 

    cout << "This vector has " << data.size() << " numbers.\n"; 
} 

void offerHelp() 
{ 
    cout << "Do you want help? "; 
    string help; 
    cin >> help; 
    if (help == "n" || help == "no"){ 
     cout << endl; 
    }else{ 
     cout << "Enter your data. Negative numbers will be added as 0. Ctrl-D to finish inputing values.\n"; 
    } 
} 

int main() 
{ 
    offerHelp(); 
    getData(); 

    cout << "How many numbers would you like to sum?"; 
    int howMany; 
    cin >> howMany; 
    computeSum();                                             

    if (success = true) { 
     cout << "The sum is " << total << endl; 
    } else { 
     cerr << "Oops, an error has occured.\n"; 
    } 

    cout << endl; 
    return 0; 
} 

답변

3

함수 선언과 같이 정의된다

void getData (벡터 &, &)

당신으로 요구하고있다 :) (

GetData의;

분명히, 컴파일러는 더 parameteres 따라서 no mathching function 오류를 취하지 않는 기능을 찾을 수 없습니다.

동일 내용은 computeSum()입니다. main에 액세스 할 수 있지만 어느 곳 main 내부에 선언되는 두 변수는

뿐만 아니라 successtotal 같은 다른 오류의 호스트가 있습니다 있습니다.

1

당신은 예를 들어, 당신의 기능에 매개 변수를 전달 잊고, :

void computeSum(vector<T> data, int n, T &out, bool &success) 

computeSum(); 

은 분명히 함수 서명이 일치하지 않습니다. 또한 클래스 T는 템플릿 클래스로 선언되지 않습니다. 나는 이것이 당신이 원래 의도 한 것이라고 생각합니다. computeSum 및 getData 함수는 클래스의 public 멤버 함수를 구현하지 않습니다.

관련 문제