2015-11-29 64 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++标签......但也许他被迫使用指针和数组。 –

相关问题