我不明白这些错误是什么意思

时间:2020-04-11 08:08:26

标签: c++ class

    #include <iostream>
    #include <sstream>
    #include <cstring>
    using namespace std;

    class Student {
        int age;
        char first_name[30];
        char second_name[30];
        int standard;
        public:
        void set_age(int a) {
            age=a;
        }
        void set_standard(int b){
            standard=b;
        }
        void set_last_name(char temp[]) {
            strcpy(second_name,temp);
        }
        void get_age() {
            cout<<age;
        }
        void get_last_name(){
            cout<<second_name;
        }
        void get_first_name() {
            cout<<first_name;
        }
        void get_standard(){
            cout<<standard;
        }
    };

    void Student::set_first_name(char temp[]){
        strcpy(first_name,temp);
    }

    int main() {
        int age, standard;
        string first_name, last_name;

        cin >> age >> first_name >> last_name >> standard;

        Student st;
        st.set_age(age);
        st.set_standard(standard);
        st.set_first_name(first_name);
        st.set_last_name(last_name);

        cout << st.get_age() << "\n";
        cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
        cout << st.get_standard() << "\n";
        cout << "\n";
        cout << st.to_string();

        return 0;
    }


错误

Solution.cpp:35:6: error: no declaration matches ‘void Student::set_first_name(char*)’
 void Student::set_first_name(char temp[]){
      ^~~~~~~
Solution.cpp:35:6: note: no functions named ‘void Student::set_first_name(char*)’
Solution.cpp:6:7: note: ‘class Student’ defined here
 class Student {
       ^~~~~~~
Solution.cpp: In function ‘int main()’:
Solution.cpp:48:8: error: ‘class Student’ has no member named ‘set_first_name’; did you mean ‘get_first_name’?
     st.set_first_name(first_name);
        ^~~~~~~~~~~~~~
        get_first_name
Solution.cpp:49:31: error: no matching function for call to ‘Student::set_last_name(std::__cxx11::string&)’
     st.set_last_name(last_name);
                               ^
Solution.cpp:18:10: note: candidate: ‘void Student::set_last_name(char*)’
     void set_last_name(char temp[]) {

5 个答案:

答案 0 :(得分:4)

您已经提供了方法的定义,但是该方法未在类中声明。

在Student类的内部 中添加以下行以提供声明:

void set_first_name(char temp[]);

答案 1 :(得分:3)

您不在类中声明函数

将此行添加到类正文中,错误将消失

void set_first_name(char temp[]);

答案 2 :(得分:1)

您有几个问题:

cout无法打印void

您尚未声明函数,但尝试定义它。

#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;

class Student {
    int age;
    char first_name[30];
    char second_name[30];
    int standard;
    public:
    void set_age(int a) {
        age=a;
    }
    void set_standard(int b){
        standard=b;
    }
    void set_last_name(const char* temp) {
        strcpy(second_name,temp);
    }
    void get_age() {
        cout<<age;
    }
    void get_last_name(){
        cout<<second_name;
    }
    void get_first_name() {
        cout<<first_name;
    }
    void get_standard(){
        cout<<standard;
    }
    void set_first_name(const char* temp){
    strcpy(first_name,temp);
}
};



int main() {
    int age, standard;
    string first_name, last_name;

    cin >> age >> first_name >> last_name >> standard;

    Student st;
    st.set_age(age);
    st.set_standard(standard);
    st.set_first_name(first_name.c_str());
    st.set_last_name(last_name.c_str());

    st.get_age();
    st.get_last_name();
    st.get_first_name();
    st.get_standard();
    cout << "\n";
    //cout << st.to_string();

    return 0;
}

答案 3 :(得分:0)

您的学生班级减速时,您需要有类似void set_first_name(char temp[])的东西。另外,字符串与char数组不同,必须将其强制转换为该类型。我建议在类中使用字符串,而不要在char数组中使用。这将导致您的最后几个错误。

答案 4 :(得分:0)

我想补充一下遗忘的答案,即您也不能引用first_name,second_name。 First_name和second_name是指向字符数组第一个元素的指针。打印它们将导致仅在控制台中打印其地址。 您必须更改以下函数声明:

void get_last_name(){
    cout<<second_name; 
} 
void get_first_name(){
    cout<<first_name;
}

您需要将'\ 0'(字符串字符的结尾)添加到字符数组,然后将其打印为字符串。通过搜索查看操作方法。您将学到很多有关字符数组和字符串的工作。