я не могу понять, что означает эта ошибка - PullRequest
0 голосов
/ 11 апреля 2020
    #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 ]

4 голосов
/ 11 апреля 2020

Вы предоставили определение метода, но метод не объявлен в классе.

Добавьте эту строку внутри класса Student, чтобы обеспечить объявление:

void set_first_name(char temp[]);
3 голосов
/ 11 апреля 2020

вы не объявляете свою функцию в своем классе

добавьте эту строку в тело класса, и ошибка исчезнет

void set_first_name(char temp[]);
1 голос
/ 11 апреля 2020

У вас есть несколько проблем:

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;
}
0 голосов
/ 11 апреля 2020

Я хочу добавить к ответу Обливиона, что вы не можете также указывать имя_символа, имя_сервера. First_name и second_name являются указателями на первый элемент массива символов. Печать их приведет к простой печати их адресов в консоли. Вы должны изменить эти объявления функций:

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

Вам необходимо добавить '\ 0' (символ конца строки) к массивам символов и затем распечатать его как строку. Проверьте, как это сделать, выполнив поиск. Вы узнаете много нового о работе с массивами символов и строками.

0 голосов
/ 11 апреля 2020

Вам нужно иметь что-то вроде void set_first_name(char temp[]) в замедлении вашего ученического класса. Кроме того, строки не совпадают с массивами символов и должны быть приведены к этому типу. Я рекомендую использовать строки в вашем классе, а не массивы символов. Это будет причиной ваших последних нескольких ошибок.

...