2016-08-03 97 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