Reputation: 44
#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;
}
errors
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[]) {
Upvotes: 0
Views: 818
Reputation: 109
I want to add to Oblivion's answer that you cannot cout first_name, second_name as well. First_name and second_name are pointers to the first element of the character array. Printing them would lead to just printing their addresses in the console. You have to change these function declarations:
void get_last_name(){
cout<<second_name;
}
void get_first_name(){
cout<<first_name;
}
You need to add '\0' (end of string character) to the character arrays and then print it as a string. Check out how to do it by searching. You will learn a lot about working of character arrays and string.
Upvotes: 0
Reputation: 350
You need to have something like void set_first_name(char temp[])
in the deceleration of your student class. Also, strings are not the same as char arrays and must be casted to that type. I recommend using strings in your class and not char arrays. This would be causing your last few errors.
Upvotes: 0
Reputation: 7374
You have several issues:
cout
cannot print void
.
you haven't declared a function but you try to define it.
#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;
}
Upvotes: 1
Reputation: 110
you don't declare your function in your class
add this line to class body and the error will disappear
void set_first_name(char temp[]);
Upvotes: 3
Reputation: 14169
You have provided a definition of the method, but the method is not declared in the class.
Add this line inside the Student class to provide a declaration:
void set_first_name(char temp[]);
Upvotes: 4