2015-11-29 102 views
-1

如何將字符串數組的內容複製到結構中?獲取錯誤,無法將類型字符串轉換爲字符串類型。最後一個循環是我遇到麻煩的地方。我是否也需要在堆上爲字符串數組分配空間?我把它分配給了分數。我以爲一個字符串是一個真正的字符數組,所以我很困惑如何使用指針來引用和傳輸它們。如何將字符串數組複製到結構中

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

    struct StudentRecords 
    { 
    string* namesRec; 
    int** examsptr; 
    }; 


    void main() 
    { 


const int NG = 4; 

string names[] = { "Amy Adams", "Bob Barr", "Carla Carr", 
        "Dan Dobbs", "Elena Evans" }; 

int exams[][NG] = 
{ 
    { 98,87,93,88 }, 
    { 78,86,82,91 }, 
    { 66,71,85,94 }, 
    { 72,63,77,69 }, 
    { 91,83,76,60 } 
}; 

string *nameHolder = nullptr; 


StudentRecords *data = new StudentRecords(); 
data->examsptr = new int*[NG]; 

for (int i = 0; i < NG; ++i) 
{ 
    data->examsptr[i] = new int[NG]; 
} 

for (int count = 0; count < NG; count++) 
{ 
    for (int count2 = 0; count2 < NG; count2++) 
     { 
     (*data).examsptr[count][count2] = exams[count][count2]; 
     cout << (*data).examsptr[count][count2] << "   " << exams[count][count2] << endl; 
     } 
    } 

    for (int count3 = 0; count3 < 5; count3++) 
    { 
    *nameHolder = names[count3]; 
    (*data).namesRec[count3] = *nameHolder; 
    cout << (*data).namesRec[count3] << endl; 
    } 
+0

Btw。 '(* data).'與'data->'相同' – bytecode77

+1

'nameHolder'是一個空指針,然後你寫'* nameHolder =' –

+0

爲什麼沒有'StudentRecords'指向現有的數組,而是試圖複製 –

回答

0

不要使用原始指針來表示數組。

不要試圖自己管理newdelete,它只是錯誤容易,正如您的代碼所證實的那樣。

請改用C++標準類庫提供的相應容器類,如std::vectorsmart pointers

如果你的教授或TA否認這樣做,就離開課程,他們沒有教C++。

0

試試這個代碼:

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

struct StudentRecords 
{ 
    char* namesRec; 
    int** examsptr; 
}; 


void main(void) 
{ 
    const int NG = 4; 

    char *names[] = { "Amy Adams", "Bob Barr", "Carla Carr", 
         "Dan Dobbs", "Elena Evans" }; 

    int exams[][NG] = 
    { 
     { 98,87,93,88 }, 
     { 78,86,82,91 }, 
     { 66,71,85,94 }, 
     { 72,63,77,69 }, 
     { 91,83,76,60 }, 
    }; 

    char *nameHolder = nullptr; 
    nameHolder = (char*)malloc(50*sizeof(char)); 

    struct StudentRecords *data = new StudentRecords(); 
    data->examsptr = new int*[NG]; 

    for (int i = 0; i < NG; i++) 
    { 
     data->examsptr[i] = new int[NG]; 
    } 

    for (int count = 0; count < NG; count++) 
    { 
     for (int count2 = 0; count2 < NG; count2++) 
     { 
      data->examsptr[count][count2] = exams[count][count2]; 
      printf("%d ",data->examsptr[count][count2]); 
     } 
     printf("\n"); 
    } 
    printf("\n"); 
    for (int count3 = 0; count3 < 5; count3++) 
    { 
     strcpy(nameHolder,names[count3]); 
     data->namesRec = (char*)malloc(30*sizeof(char)); 
     strcpy(data->namesRec,names[count3]); 
     printf("%s ",data->namesRec); 
     free(data->namesRec); 
    } 

    for (int i = 0; i < NG;i++) 
    { 
     delete data->examsptr[i]; 
    } 
    delete data; 
    free(nameHolder); 
} 
+0

你不應該混合使用'malloc'和'new'。在某些時候,您可能會在'new'ed資源上調用'malloc'ed資源上的'delete'或'free',然後它會成爲未定義行爲。 –

+0

這是如此糟糕的編碼,在C++中,從來不使用'malloc',如果我們做'malloc',必須使用'free()'。和C++有'new'和'delete'! –

1

必須初始化data->namesRec = new string[size];因爲是指針!

0

只是一個提示:

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

struct Student { 
    std::string name; 
    std::vector<int> exams; 
}; 


int main() { 
    std::vector<Student> sList{ {"Amy Adams", { 98,87,93,88 }}, 
           {"Bob Barr", { 78,86,82,91 }}, 
           {"Carla Carr", { 66,71,85,94 }}, 
           {"Dan Dobbs", { 72,63,77,69 }}}; 

    std::string sname = "Elena Evans"; // Just arrived... 
    int exms[] = { 91,83,76,60 };   // if you need to use an array 
    sList.push_back(Student{sname, {exms,exms+sizeof(exms)/sizeof(int)}}); 

    // show students 
    for (auto & s : sList) {  // for every element of the vector 
     std::cout << s.name << ": \t"; 
     for (auto & e : s.exams) std::cout << e << " "; 
     std::cout << std::endl; 
    } 

    return 0; 
} 

如果你不能因爲某些原因使用標準庫中的容器和設施,你至少可以試着模仿他們的能力和寫一些功能來管理副本,分配和dellocations:

std::string * _AllocNames(size_t n) { 
    std::string * new_ptr; 
    try { 
     new_ptr = new std::string[n]; 
     return new_ptr; 
    } 
    catch (std::bad_alloc &ba) {  // Something bad happened 
     std::cerr << "Error, exception caught: " << ba.what() << '\n'; 
     exit(1); 
    } 
} 

void _DeAllocNames(std::string * names) { 
    if (names) { 
     for (size_t i = 0; i < nStd; i++) names[i].clear(); 
     delete[] names; 
    } 
} 

int ** _AllocExams(size_t n, size_t m) { 
    int ** new_ptr; 
    try { 
     new_ptr = new int*[n]; 
     for (size_t i = 0; i < n; i++) new_ptr[i] = new int[m]; 
     return new_ptr; 
    } 
    catch (std::bad_alloc &ba) {  // Something bad happened 
     std::cerr << "Error, exception caught: " << ba.what() << '\n'; 
     exit(1); 
    } 
} 


void _DeAllocExams(int ** exams) { 
    if (exams) { 
     for (size_t i = 0; i < nStd; i++) delete[] exams[i]; 
     delete[] exams; 
    } 
} 

對於副本,你需要記住int **與int [] [4]不是同一個東西。

void _Copy(const std::string * source, size_t n, std::string * dest) { 
    for (size_t i = 0; i < n ; i++) dest[i] = source[i]; 
} 

template < size_t K > 
    void _Copy(const int source[][K], size_t n, int ** dest) { 
     for (size_t i = 0; i < n ; i++) 
      for (size_t j = 0; j < K; j++) 
       dest[i][j] = source[i][j]; 
} 

void _Copy(int ** source, size_t n, size_t m, int ** dest) { 
     for (size_t i = 0; i < n ; i++) 
      for (size_t j = 0; j < m; j++) 
       dest[i][j] = source[i][j]; 
} 
+0

我猜他不需要C++ 11,這意味着他不知道什麼是'auto' .. –

+0

如果他正在使用'nullptr',那麼使用C++ 11功能大概可以。 –

+0

那麼,我看到了一個C++標籤......但也許他被迫使用指針和數組。 –

相關問題