在C++中,我有一个名为“Student.h”的文件

class LinkedList {
private:

class Student {
public:

    int stId;
    char stName [20];
    char stMajor[20];
    double stAverage;
    Student * next;

    Student() {
        next = 0;
    }

    Student(int stId, char stName [20], char stMajor[20], double stAverage) {
        this.stId = stId;
        strcopy(this.stName, stName); // there is error here !
        strcopy(this.stMajor, stMajor);
        this.stAverage = stAverage;
    }

我该怎么办 ?!

最佳答案

this是C++中的指针,而不是Java中的引用。另外,您需要的是strcpy()而不是strcopy()
试试这个

    strcpy(this->stName, stName);
    strcpy(this->stMajor, stMajor);

P.S:在C++中,始终建议使用std::string而不是C样式的数组

您的代码要干净得多,就像这样
struct Student {

    int stId;
    std::string stName;
    std::string stMajor;
    double stAverage;
    Student * next;

    Student():stId(),stAverage(),next()//prefer initialization-list to assignment
    {
    }

    Student(int stId, const std::string &stName, const std::string &stMajor, double stAverage){
      this->stId = stId,
      this->stName = stName ,
      this->stMajor = stMajor,
      this->stAverage = stAverage;
    }
};

关于c++ - 如何在C++中使用strcpy,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4422560/

10-15 05:18