2016-09-25 101 views
0

我對C++很新,需要一些關於下面代碼的幫助。返回一個結構指針

#include <iostream> 
using namespace std; 

struct student { 
    string name; 
    int age; 
    float marks; 
}; 

struct student *initiateStudent(string , int , float); 

int main () { 
    int totalStudents = 1; 
    string name; 
    int age; 

    float marks; 

    cin >> totalStudents; 

    student *stud[totalStudents]; 

    for(int i = 0; i < totalStudents; i++) { 
     cin >> name >> age >> marks; 
     stud[i] = initiateStudent(name,age,marks); 
    } 
    cout << stud[0]->name; 

    return 0; 
} 

struct student *initiateStudent(string name, int age, float marks) 
{ 
    student *temp_student; 

    temp_student->name = name; 
    temp_student->age = age; 
    temp_student->marks = marks; 
    return temp_student; 
} 

我需要在功能initiateStudent通過將會員年齡,標記一個結構指針返回指針數組螺柱。 我知道問題的根源在於,當我返回到主文件時temp_student被銷燬。 所以我的問題是如何通過傳遞結構的成員,然後返回信息返回到指針數組螺柱。

非常感謝。

+0

'student * temp_student;'錯過了分配:您在這裏有未定義的行爲。 –

+0

您正在使用哪本書來學習C++?你似乎已經撿到了一些壞習慣:( –

+0

'code.c:10:3​​:警告 - 「學生」在代碼中發現 - 可能的星期天晚上的作業 - 當回答時預計downvoting :) :) –

回答

0

簡單的解決方案是讓你的initiateStudent()在堆上創建temp_student(使用new):並返回它。記住堆分配的內存不會自動釋放,所以不要忘記稍後釋放它自己。

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

struct student { 
    string name; 
    int age; 
    float marks; 
}; 

struct student *initiateStudent(string , int , float); 

int main () { 
    int totalStudents = 1; 
    string name; 
    int age; 

    float marks; 

    cout << "Total student: "; 
    cin >> totalStudents; 
    cin.sync(); // very very important to not affect the next input (name) 

    student* stud = new student[totalStudents]; 

    for(int i = 0; i < totalStudents; i++) 
    { 
     cout << "Name: "; 
     getline(cin, name); 
     cin.sync(); 
     cout << "age: "; 
     cin >> age; 
     cout << endl; 
     cout << "Marks: "; 
     cin >> marks; 
     cout << endl; 
     cin.sync(); 

     stud[i] = *initiateStudent(name, age, marks); 
    } 

    cout << "Student 1: " << stud[0].name << endl; 

    delete[] stud; 
    stud = NULL; 

    return 0; 
} 

struct student *initiateStudent(string name, int age, float marks) 
{ 
    student *temp_student = new student; 

    temp_student->name = name; 
    temp_student->age = age; 
    temp_student->marks = marks; 
    return temp_student; 
} 
+0

'student * stud [] =新生(*)[totalStudents];'是否應該? –

+0

Nvm,我看你deref返回 –

+0

當你在它,重寫代碼使用'std :: vector'。壞習慣很難消退。 –

2

半的答案解釋的壞習慣:

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

//using namespace std; often injects subtle bugs. Use with caution 
// read more here: 
// http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice 


struct student 
{ 
    std::string name; // explicit namespacing reduces possibility of unwanted collisions 
    int age; 
    float marks; 
    //added constructor in place of initialization function. 
    student(std::string name, int age, float marks):name(name), age(age), marks(marks) 
    { 
    } 
}; 

int main() 
{ 
    int totalStudents = 1; 
    std::string name; 
    int age; 

    float marks; 

    while (!(std::cin >> totalStudents)) // testing input for success 
             // Needed extra brackets caught by M.M 
             // teach me to not test even a throw-away example  
    { 
     std::cout << "must... have... good... input..." << std::endl; 
     cin.clear(); // clear the error and get rid of any other garbage the user may have input. 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

    } 

    //student *stud[totalStudents]; illegal in C++ 
    std::vector<student *> stud(totalStudents); // using dynamic array instead 

    for (int i = 0; i < totalStudents;)// i++ removed 
    { 
     if (std::cin >> name >> age >> marks) //testing input 
     { 
      stud[i] = new student(name, age, marks); // using constructor 
      i++; // and put here. Only increment if input is valid. 
     } 
     else 
     { 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     } 
    } 
    std::cout << stud[0]->name; 

    for (student * stu: stud) // cleaning up allocated memory 
    { 
     delete stu; 
    } 

    return 0; 
} 

一個C的美女++是很少需要自我管理內存。事實上有huge advantages in not doing it以上和以後不需要自己清理。

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


struct student 
{ 
    std::string name;  
    int age; 
    float marks; 

    student(std::string name, int age, float marks):name(name), age(age), marks(marks) 
    { 
    } 
}; 

int main() 
{ 
    std::string name; 
    int age; 
    float marks; 

    std::vector<student> stud; // look ma! No pointer! 

    while (std::cin >> name >> age >> marks) //exits loop on bad input 
    { 
     stud.emplace_back(name, age, marks); // building directly in vector 
              // vector will size itself as needed. 
    } 
    std::cout << stud[0].name; 

    return 0; 
} 

還有一點需要注意:>>是以空格分隔的。這意味着當它找到空格(空格,製表符,行尾......)時,它停止,因此「John Jacob Jingleheimer-Shmidt」的名字將以「John」的名義命名。然後>>將試圖將「雅各布」解釋爲age,那將不會那麼順利。

+0

只是一個問題,雖然:是for(int i = 0; i MayeulC

+1

這是個人的偏好。說實話,我會清理那個'int i = 0;而(我 user4581301

相關問題