2016-08-03 83 views
2

我想創建一個使用類,數組和函數來顯示關於兩個學生(名稱,ID#,註冊的類)信息的程序。我正在努力的部分是將數組傳遞給函數。我怎麼做?將字符串數組傳遞給函數

#include <string> 
#include <iostream> 
#include <iomanip> 
using namespace std; 

class Student // Student class declaration. 
{ 
private: 
    string name; 
    int id; 
    string classes; 
    int arraySize; 

public: 
    void setName(string n) 
    { 
    name = n; 
    } 
    void setId(int i) 
    { 
    id = i; 
    } 
    void setClasses(string c, int num) 
    { 
    classes = c; 
    arraySize = num; 
    } 
    string getName() 
    { 
    return name; 
    } 
    int getId() 
    { 
    return id; 
    } 
    void getClasses() 
    { 
    for (int counter=0; counter <arraySize; counter++) { 

     cout << classes[counter] << endl; 
    } 

    } 

}; 

int main() 
{ 
    //Student 1 
    string s1Name = "John Doe"; 
    int s1Id = 51090210; 
    int const NUMCLASSES1 = 3; 
    string s1Classes[NUMCLASSES1] = {"C++","Intro to Theatre","Stagecraft"}; 
    //Student 2 
    string s2Name = "Rick Harambe Sanchez"; 
    int s2Id = 666123420; 
    int const NUMCLASSES2 = 2; 
    string s2Classes[NUMCLASSES2] = {"Intro to Rocket Science","Intermediate Acting"}; 
    // 

    Student info; 

    info.setName(s1Name); 
    info.setId(s1Id); 
    //info.setClasses(s1Classes, NUMCLASSES1); 
    cout << "Here is Student #1's information:\n"; 
    cout << "Name: " << info.getName() << endl; 
    cout << "ID: " << info.getId() << endl; 
    //cout << "Classes: " << info.getClasses() << endl; 


    info.setName(s2Name); 
    info.setId(s2Id); 
    // info.setClasses(s2Classes, NUMCLASSES1); 
    cout << "\n\nHere is student #2's information:\n"; 
    cout << "Name: " << info.getName() << endl; 
    cout << "ID: " << info.getId() << endl; 
    //cout << "Classes: " << info.getClasses() << endl; 



    return 0; 
} 
+2

作爲初學者,更喜歡'std:.vector'到原始數組。傳遞'std :: vector'與傳遞'int'或'std :: string'相同。 –

+0

在這段代碼中你試圖將數組傳遞給函數?你可以傳遞一個指針,但是你也應該傳遞一個長度。你可以使用'std :: array'或者'vector'來象其他許多人所說的那樣。 –

回答

0

在課堂上學生的私有變量,您存儲的字符串: String classes; 哪裏,你應該保存像是一個字符串數組: String classes[MAX_NUM_CLASSES];

然後在集類的功能,通過在字符串作爲第一個參數數組,所以它應該是:

void setClasses(string[] c, int num) 

{ 

classes = c; //not sure if simply setting them equal will work, rather copy entire array using a for loop 

arraySize = num; 

} 

這應該指向你在正確的方向

此外,使用std::vector而不是string[],它會更容易。

0

可以傳遞的any_data_type陣列function這樣

void foo(data_type arr[]); 

foo(arr); // If you just want to use the value of array 
foo(&arr); // If you want to alter the value of array. 
0

的常用方法以繞過可變長度列表在C++是使用std::vector。 A vector是一個單一對象,您可以輕鬆傳遞給某個函數,複製(或引用)其內容。如果您熟悉Java,則基本上是ArrayList。這裏是一個例子:

#include <vector> 
#include <string> 
using namespace std; 

class foo { 
private: 
    vector<string> myStrings; 

public: 
    void setMyStrings(vector<string> vec) { 
    myStrings = vec; 
    } 
} 

//... 

foo myObj; 
vector<string> list = {"foo","bar","baz"}; 
myObj.setMyStrings(list); 

如果不想使用標準庫,但可以傳遞一個數組的C風格。這涉及將指針傳遞給數組的第一個元素以及數組的長度。例如:

void processStrings(string* arr, int len) { 
    for (int i = 0; i < len; i++) { 
    string str = arr[i]; 
    //... 
    } 
} 

string array[] = {"foo","bar","baz"}; 
processStrings(array, 3); // you could also replace 3 with sizeof(array) 

像這樣傳遞原始數組,尤其是如果您想將數組複製到對象中,可能會很痛苦。 Raw數組 in C & C++只是指向列表的第一個元素的指針。與Java和JavaScript等語言不同,它們不會記錄它們的長度,也不能只將一個數組分配給另一個數組。 std::vector封裝了「事物列表」的概念,並且通常更直觀地用於該目的。

生活教訓:使用std :: vector。

編輯:請參閱@ nathanesau的答案爲使用構造函數更清楚地初始化對象的示例。 (但是不要複製粘貼,自己寫下來!你會以這種方式學得更快。)

0

使用std::vector。另外,不要添加你不需要的功能。下面是一個使用示例std::vector

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

using std::string; 
using std::vector; 

class Student // Student class declaration. 
{ 
private: 

    vector<string> classes; 
    string name; 
    int id; 

public: 

    Student (const vector<string> &classesUse, string nameUse, int idUse) : 
     classes (classesUse), 
     name (nameUse), 
     id  (idUse) 
    { 
    } 

    void print() 
    { 
     std::cout << "Name: " << name << std::endl; 
     std::cout << "Id:  " << id << std::endl; 
     std::cout << "Classes: "; 

     for (int i = 0; i < classes.size(); i++) 
     { 
      if (i < classes.size() - 1) 
      { 
       std::cout << classes[i] << ", "; 
      } 
      else 
      { 
       std::cout << classes[i] << std::endl; 
      } 
     } 

     std::cout << std::endl; 
    } 
}; 

int main() 
{ 
    Student John ({"C++","Intro to Theatre","Stagecraft"}, 
        "John", 
        51090210); 

    John.print(); 

    Student Rick ({"Intro to Rocket Science","Intermediate Acting"}, 
        "Rick", 
        666123420); 

    Rick.print(); 

    return 0; 
} 


Name: John 
Id:  51090210 
Classes: C++, Intro to Theatre, Stagecraft 

Name: Rick 
Id:  666123420 
Classes: Intro to Rocket Science, Intermediate Acting