Good day! I am writing a simple class in C ++ when compiling I stumbled upon a strange problem like this .. I know little about C ++, but here is my .h and .cpp files:

If there are errors in the spelling of the code, I ask you to leave notes for the future like: you cannot write in C ++ like that, but you should like this

Student.cpp

 #include "Student.h" #include <string> using std::string; Student::Student(){} void Student::setName(string n){ name = n; } void Student::setSurname(string s){ surname = s; } void Student::setAge(int a){ age = a; } void Student::setCourse(int c) { course = c; } void Student::setGroup(string g){ group = g; } void Student::setMarks(string* mark){ marks = mark; } string Student::getName(){ return name; } string Student::getSurname(){ return surname; } int Student::getAge(){ return age; } int Student::getCourse(){ return course; } string Student::getGroup(){ return group; } string* Student::getMarks(){ return marks; } 

Student.h

 #ifndef Student_h #define Student_h #include <string> using std::string; class Student{ public: Student(); void setName(string); string getName(); void setSurname(string); string getSurname(); void setAge(int); int getAge(); void setCourse(int); int getCourse(); void setGroup(string); string getGroup(); void setMarks(string*); string* getMarks(); private: string name; string surname; int age; int course; string group; string* marks; }; #endif 

Main.cpp

 #include <stdio.h> #include <iostream> #include "Student.h" using std::cout; using std::endl; int main(){ Student test; string marks[] = {"B","B","C","A","A","A"}; test.setName("Name"); test.setSurname("Surname"); test.setAge(100); test.setCourse(100); test.setGroup("606.4"); test.setMarks(marks); cout << test.getName() << endl; cout << test.getSurname() << endl; cout << test.getAge() << endl; cout << test.getCourse() << endl; cout << test.getGroup() << endl; string* mark = test.getMarks(); for(int i = 0 ; i < 6 ; i++){ cout << mark[i] <<" "; } } 

It seems to be doing everything right, but a strange compilation error occurs ...

This is when compiling Main.cpp

This is when compiling the Main method.

And this is when compiling Student.cpp

Student Compilation

Tell me where all my blunders?

  • Linker error. It seems that only the main.cpp file is compiled, but not the student.cpp file. Is the student.cpp file actually added to the project? - Andrey Kuruljov
  • @KryTer_NexT It seems that instead of a project of a console application, you are creating a Windows application. In addition, the object module with the definitions of the class Student is not connected to the project, and therefore all the definitions contained in it are not available. - Vlad from Moscow
  • @VladfromMoscow So I need to build this all in a project? I did all this in one folder and tried to run it - E1mir

1 answer 1

As for why it is not compiled - as already mentioned, your errors are linking errors, so this is the Student.cpp file that is not connected to the project, and WinMain that is not found is saying that you are trying to create a window application, and not a console application.

But you have the second part of the question - how to write. You have no errors - in the sense that they will cause the program to fail. But, just like you formulated - it’s not worth writing in C ++ ...

I would start with the fact that you have in fact not a class, but a structure. In the sense that your class does not contain any specific internal state that should be necessarily maintained in a consistent state, or some other functionality besides setting and returning fields. Therefore, I personally do not see a big sin in simply declaring your class a structure (that is, a class with all open members) and working directly with them:

 struct Student{ string name; string surname; int age; int course; string group; string* marks; }; ... Student test; test.name = "Name"; 

If some more serious work is planned, or by assignment it is required that it be a class - then it’s better to make such small one-line functions inline (inline), i.e. define them directly in the class declaration. This will allow the compiler not to call them, but simply embed it in the code, which has a beneficial effect on performance. And yet - I would make all return functions constant - so that they can be called for constant objects. I would pass strings as constant links to copy less.

 class Student{ public: Student(); void setName(const string& s) { name = s; } string getName() const { return name; } void setSurname(const string& s) { surname = s; } string getSurname() const { return surname; } ... private: string name; string surname; int age; int course; string group; string* marks; }; 

Another thing is that your assessments are clearly single-character, so I would not fence an array of strings, but use just a string — which, in general, is an array of characters (I would add the function of adding an estimate — that is, joining this string of the next character). In addition, you would get rid of what is called the "magic constants" - you would not need to remember that when evaluating this, the ratings are 6:

 for(char c : mark){ cout << c <<" "; } 

And yet - you copy all the lines into the class, which is correct - the object turns out to be self-sufficient, “all your burden with you”. You simply pass this array as a pointer to an entity external to the class. and if it changes - it changes, it turns out, and the state of the class. Roughly speaking, your student's record-keeping record is not a record-book, but a cover with a piece of paper on which is written - “but my grades are written right there on the blackboard” - where any hooligan can correct them ... Ie even if you are working with an array of strings, it is better to copy it inward, for greater encapsulation.

And yet - why don't you create another designer who would create a named student at once? It is unlikely that the name and surname will change (unless the student gets married :)).

  • Hmm, the answer is super, yes, I agree with you about the structure, but I was given the assignment to create a class ... I didn’t really understand about the ratings, can you tell us more? - E1mir
  • Get string marks; and functions like setMarks(сщтые string& m) , as for other strings, and Student::addMark(char m) { marks += m; } Student::addMark(char m) { marks += m; } . You can then assign grades either as test.setMarks("ABBCAB") , or as test.addMark('A'); test.addMark('B'); ... test.addMark('A'); test.addMark('B'); ... test.addMark('A'); test.addMark('B'); ... - Harry
  • Aaa understood :)) Now I’ll check everything and try to do it) - E1mir
  • The function setMark where do I write? In the Student.cpp, header file or in the Main'e? - E1mir 2:51 pm
  • This is a Student member, and where you write is your business :) - Harry