2016-12-03 54 views
-2

我在將向量作爲參數傳遞給類函數時遇到了一些問題。類在另一個文件中。這是我的代碼:未傳遞給類函數的字符串向量

main.cpp中:

#include <iostream> 
#include <vector> 
#include <string> 

#include "class1.h" 

using namespace std; 

int main() { 

    vector<string> names; 

    names.push_back("Bob"); 
    names.push_back("Gorge"); 
    names.push_back("Bill"); 
    names.push_back("Freddy"); 
    names.push_back("Daniel"); 
    names.push_back("Emily"); 


    class1 obj; 
    obj.printVector(names); 

    system("pause"); 
} 

class1.h:

#pragma once 

class class1 
{ 
public: 
    void printVector(std::vector<string>& names); 
}; 

class1.cpp:

#include <string> 

#include "class1.h" 

using namespace std; 

void class1::printVector(vector<string>& names) { 

    for (unsigned int i = 0; i < names.size(); i++) 
    { 
     cout << names[i] << endl; 
    } 
} 

我試圖做同樣的事情與其他數據類型(int,char,float ...)和它的工作。當我只是將它們傳遞給main.cpp中的函數時,它就可以工作。錯誤是:

- Severity Code Description Project File Line Suppression State 
Error C3203 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C3203 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C2923 'std::vector': 'string' is not a valid template type argument for parameter '_Ty' tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C2923 'std::vector': 'string' is not a valid template type argument for parameter '_Ty' tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C2065 'string': undeclared identifier tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C2065 'string': undeclared identifier tesst c:\users\...\documents\programs and games\tesst\tesst\class1.h 6 

- Severity Code Description Project File Line Suppression State 
Error C2664 'void class1::printVector(std::vector &)': cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'std::vector &' tesst c:\users\...\documents\programs and games\tesst\tesst\main.cpp 22 

- Severity Code Description Project File Line Suppression State 
Error C2511 'void class1::printVector(std::vector<std::string,std::allocator<_Ty>> &)': overloaded member function not found in 'class1' tesst c:\users\...\documents\programs and games\tesst\tesst\class1.cpp 9 

請幫忙! 謝謝

回答

-1

你的問題不是傳遞參數給一個類。 下面的代碼在這裏工作:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 

class class1 { 
public: 
    void printVector(std::vector<string>& names); 

}; 

void class1::printVector(vector<string>& names) { 

    for (unsigned int i = 0; i < names.size(); i++) 
    { 
     cout << names[i] << endl; 
    } 
} 

int main() { 

    vector<string> names; 

    names.push_back("Bob"); 
    names.push_back("Gorge"); 
    names.push_back("Bill"); 
    names.push_back("Freddy"); 
    names.push_back("Daniel"); 
    names.push_back("Emily"); 


    class1 obj; 
    obj.printVector(names); 
     return 0; 
} 

如軌道之前亮度競賽中提到,你的問題是不包括向量和字符串到Class

+0

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – juanchopanza

+0

我剛纔說明,如果問題是傳遞一個矢量到一個函數,代碼會打破:)但感謝分享鏈接@juanchopanza –

1

class1.h不包括<string><vector>.

你的事實,你只是這麼碰巧包含在源文件中<string>包括class1.h前(前者不作爲「與越來越遠」不是你應該依賴的東西!)。

在後一種情況下,class1.cpp完全不知道vector的含義。

修復您的包含。

+0

不錯!提交之前我沒有看到您的答案。這是更準確的答案:) –

+0

非常感謝你的幫助。 – jose2000

+0

歡迎來到StackOverflow @ jose200。你應該upvote並接受這個答案,因爲它對你有用:) –